import math
import pandas as pd
import numpy as np
from datetime import datetime
from utils.common import get_holidays, get_current_time_type, fea_div_v2, get_days_inter, find_max_consecutive


def if_first_order(x):
    if np.isnan(x):
        return 0
    elif x==21:
        return 2
    else:
        return 1


def if_first_order_new(item_df):
    """
    当前订单是否是首单 还是再次单，
    定义为，当前单之前上一笔如果被拒绝，则当前单为首单，如果上一笔通过，则为再次单
    """
    current_is_first_order = 1  # 当前订单是否是首单 1 是首单 2 是再次单 0 是在当前产品是时间首单, 默认是首单 , 根据不同数据进行修改
    if len(item_df) == 0:  # 如果没有上一笔默认是0
        current_is_first_order = 0
    else:
        last_order = item_df.iloc[0]  # 上一笔订单
        last_status = last_order.status
        if last_status == 21:  # 如果上一笔是成功的则为再次单
            current_is_first_order = 2
    return current_is_first_order


def get_current_order_features(user_history_order,
                               current_apply_time,
                               register_time,
                               current_acq_channel,
                               current_product_code,
                               country_id=None):
    """
    获取当前订单相关特征
    """
    current_weekday = pd.to_datetime(current_apply_time).weekday()  # 当前日期是周几
    current_time_type = get_current_time_type(current_apply_time)  # 当前时间类型晚上 上午 下午 凌晨
    current_is_holiday = get_holidays(country_id=country_id, self_date=current_apply_time)  # 当前日式是否是节假日
    st_register_time = datetime.strptime(register_time, "%Y-%m-%d %H:%M:%S")
    ed_current_apply_time = datetime.strptime(current_apply_time, "%Y-%m-%d %H:%M:%S")
    current_reg_time_seconds = (ed_current_apply_time - st_register_time).seconds  # 当前时间距离注册时间的间隔秒数
    # 获取all,本产品,其他产品,本app,其他app的订单排序数据
    all_app_df = user_history_order
    self_app_df = user_history_order[user_history_order.acq_channel == current_acq_channel]
    other_app_df = user_history_order[user_history_order.acq_channel != current_acq_channel]
    self_product_df = user_history_order[user_history_order.product_code == current_product_code]
    other_product_df = user_history_order[user_history_order.product_code != current_product_code]

    apply_current_is_first_order = if_first_order_new(all_app_df)  # 当前订单在所有all上是首单还是再次单 1为首单 2为再次单 0 为时间首单
    apply_current_self_app_is_first_order = if_first_order_new(self_app_df)  # 当前订单在当前app上是首单还是再次单 1为首单 2为再次单 0 为时间首单
    apply_current_other_app_is_first_order = if_first_order_new(other_app_df)  # 当前订单在其他app上是首单还是再次单 1为首单 2为再次单 0 为时间首单
    apply_current_self_product_is_first_order = if_first_order_new(self_product_df)  # 当前订单在当前产品上是首单还是再次单 1为首单 2为再次单 0 为时间首单
    apply_current_other_product_is_first_order = if_first_order_new(other_product_df)  # 当前订单在其他产品上是首单还是再次单 1为首单 2为再次单 0 为时间首单

    return {'apply_current_weekday': current_weekday,
            'apply_current_time_type': current_time_type,
            'apply_current_is_holiday': current_is_holiday,
            'apply_current_reg_time_seconds': current_reg_time_seconds,
            'apply_current_is_first_order': apply_current_is_first_order,
            'apply_current_self_app_is_first_order': apply_current_self_app_is_first_order,
            'apply_current_other_app_is_first_order': apply_current_other_app_is_first_order,
            'apply_current_self_product_is_first_order': apply_current_self_product_is_first_order,
            'apply_current_other_product_is_first_order': apply_current_other_product_is_first_order}


def get_first_features(user_history_order,
                       feature_type='all',
                       country_id=None,
                       current_loan_amount=None,
                       current_apply_time=None):
    # 第一次相关特征
    if feature_type == 'all':
        user_history_order = user_history_order.copy()
    elif 'app' in feature_type:
        user_history_order = user_history_order[user_history_order.app_type == feature_type].copy()
    elif 'product' in feature_type:
        user_history_order = user_history_order[user_history_order.product_type == feature_type].copy()

    if len(user_history_order) > 0:
        apply_first_time = user_history_order.iloc[-1].apply_time  # 第一次申请时间
        apply_first_reg_days = user_history_order.iloc[-1].reg_day_inter  # 第一次订单申请距离注册的时间差
        apply_current_first_days = user_history_order.iloc[-1].order_day_inter  # 第一次订单申请距离本订单的时间差
        apply_first_status = 1 if user_history_order.iloc[-1].status == 21 else 0  # 第一次申请状态 1 为通过 0为拒绝
        apply_first_amount = user_history_order.iloc[-1].loan_amount  # 第一次申请金额
        apply_first_weekday = pd.to_datetime(apply_first_time).weekday()  # 首次申请日期是周几
        apply_first_time_type = get_current_time_type(apply_first_time)  # 首次申请时间类型晚上 上午 下午 凌晨
        apply_first_is_holiday = get_holidays(country_id=country_id, self_date=apply_first_time)  # 首次申请日期是否是节假日

        # 第一次拒绝相关特征
        sub_user_history_order = user_history_order[user_history_order.status == 22]
        if len(sub_user_history_order) >0:
            first_refuse_time = sub_user_history_order.iloc[-1].apply_time  # 第一次拒绝时间
            apply_first_refuse_weekday = pd.to_datetime(first_refuse_time).weekday()  # 首次拒绝日期是周几
            apply_first_refuse_time_type = get_current_time_type(first_refuse_time)  # 首次拒绝时间类型晚上 上午 下午 凌晨
            apply_first_refuse_is_holiday = get_holidays(country_id=country_id,self_date=first_refuse_time)  # 首次拒绝日期是否是节假日
            apply_current_first_rej_days = get_days_inter(current_apply_time, first_refuse_time)  #
            # 当前申请时间距离第一次被拒绝订单的间隔天数
            apply_before_first_rej_order_cnt = user_history_order[user_history_order.apply_time < first_refuse_time].shape[0]  # 第一次被拒绝订单之前申请订单次数
            apply_before_first_rej_order_pass_cnt = user_history_order[(user_history_order.apply_time < first_refuse_time) & (user_history_order.status == 21)].shape[0]  # 第一次被拒绝订单之前放款订单次数
            apply_before_first_rej_order_pass_amt = user_history_order[(user_history_order.apply_time < first_refuse_time) & (user_history_order.status == 21)].loan_amount.sum()  # 第一次被拒绝订单之前放款金额和
            apply_after_first_rej_order_cnt = user_history_order[user_history_order.apply_time > first_refuse_time].shape[0]  # 第一次被拒绝订单之后申请订单次数
            apply_after_first_rej_order_pass_cnt = user_history_order[(user_history_order.apply_time > first_refuse_time) & (user_history_order.status == 21)].shape[0]  # 第一次被拒绝订单之后放款订单次数
            apply_after_first_rej_order_pass_amt = user_history_order[(user_history_order.apply_time > first_refuse_time) & (user_history_order.status == 21)].loan_amount.sum()  # 第一次被拒绝订单之后放款金额和
            apply_first_rej_after_before_cnt_rate = fea_div_v2(apply_before_first_rej_order_pass_cnt,apply_after_first_rej_order_pass_cnt)  # 第一次被拒绝前后的订单放款单量比
            apply_first_rej_after_before_amt_rate = fea_div_v2(apply_before_first_rej_order_pass_amt,apply_after_first_rej_order_pass_amt)  # 第一次被拒绝订单前后的订单放款金额比
        else:
            apply_first_refuse_weekday = -999
            apply_first_refuse_time_type = -999
            apply_first_refuse_is_holiday = -999
            apply_current_first_rej_days = -999
            apply_before_first_rej_order_cnt = -999
            apply_before_first_rej_order_pass_cnt = -999
            apply_before_first_rej_order_pass_amt = -999
            apply_after_first_rej_order_cnt = -999
            apply_after_first_rej_order_pass_cnt = -999
            apply_after_first_rej_order_pass_amt = -999
            apply_first_rej_after_before_cnt_rate = -999
            apply_first_rej_after_before_amt_rate = -999

        # 第一次放款相关特征
        sub_user_history_order = user_history_order[user_history_order.status == 21]
        if len(sub_user_history_order) > 0:
            first_pass_time = sub_user_history_order.iloc[-1].apply_time  # 第一次放款时间
            apply_current_first_pass_days = get_days_inter(current_apply_time, first_pass_time)  # 当前申请时间距离第一次放款订单的间隔天数
            apply_before_first_pass_order_rej_cnt = user_history_order[(user_history_order.apply_time < first_pass_time) & (user_history_order.status == 22)].shape[0]  # 第一次放款前被拒绝的订单数量
            apply_after_first_pass_order_rej_cnt = user_history_order[(user_history_order.apply_time > first_pass_time) & (user_history_order.status == 22)].shape[0]  # 第一次放款前后拒绝的订单数量

            apply_first_pass_weekday = pd.to_datetime(first_pass_time).weekday()  # 首次通过日期是周几
            apply_first_pass_time_type = get_current_time_type(first_pass_time)  # 首次通过时间类型晚上 上午 下午 凌晨
            apply_first_pass_is_holiday = get_holidays(country_id=country_id, self_date=first_pass_time)  # 首次通过日期是否是节假日

            tmp_user_history_order = user_history_order[(user_history_order.apply_time > first_pass_time) & (user_history_order.status == 22)]
            if len(tmp_user_history_order) > 0:
                first_refuse_time_sub = tmp_user_history_order.iloc[-1].apply_time # 首次放款后，第一次拒绝时间
                last_refuse_time_sub = tmp_user_history_order.iloc[0].apply_time  # 首次放款后，最近一次拒绝时间
                apply_first_pass_order_first_rej_days = get_days_inter(first_refuse_time_sub,first_pass_time)  # 首次放款后，第一次拒绝订单距离首次放款时间间隔
                apply_first_pass_order_first_rej_day_rate = fea_div_v2(apply_first_pass_order_first_rej_days,apply_current_first_pass_days)  # 首次放款后，第一次拒绝订单 距离时间差 / 本次申请时间差 - 首次放款时间
                apply_first_pass_order_last_rej_days = get_days_inter(last_refuse_time_sub,first_pass_time)  # 首次放款后，最近一次拒绝订单距离首次放款时间间隔
                apply_first_pass_order_last_rej_day_rate = fea_div_v2(apply_first_pass_order_last_rej_days,apply_current_first_pass_days)  # 首次放款后，最近一次拒绝订单 距离时间差 / 本次申请时间差 - 首次放款时间
            else:
                apply_first_pass_order_first_rej_days = -999
                apply_first_pass_order_first_rej_day_rate = -999
                apply_first_pass_order_last_rej_days = -999
                apply_first_pass_order_last_rej_day_rate = -999

            first_pass_order_id = user_history_order[(user_history_order.status == 21) & (user_history_order.apply_time == first_pass_time)].iloc[0].app_order_id  # 首次放款成功的最后一笔订单
            apply_first_pass_amt = user_history_order[user_history_order.app_order_id == first_pass_order_id].loan_amount.values[0]  # 第一次放款金额
            apply_current_first_amt = current_loan_amount - apply_first_pass_amt  # 本次订单金额  - 第一笔放款金额
            apply_current_first_amt_rate = fea_div_v2(apply_current_first_amt,apply_first_pass_amt)  # 本次订单金额 - 第一笔放款金额/ 第一笔放款金额
            first_pass_detail_df = user_history_order[user_history_order.apply_time < first_pass_time].copy()
            apply_first_pass_is_again = if_first_order_new(first_pass_detail_df)  # 第一次放款是首单还是再次单首次放款的话 全部为首单或者第一单，如果是再次单，则说明计算错误
        else:
            apply_first_pass_weekday = -999
            apply_first_pass_time_type = -999
            apply_first_pass_is_holiday = -999
            apply_current_first_pass_days = -999
            apply_before_first_pass_order_rej_cnt = -999
            apply_after_first_pass_order_rej_cnt = -999
            apply_first_pass_order_first_rej_days = -999
            apply_first_pass_order_first_rej_day_rate = -999
            apply_first_pass_order_last_rej_days = -999
            apply_first_pass_order_last_rej_day_rate = -999
            apply_first_pass_amt = -999
            apply_current_first_amt = -999
            apply_current_first_amt_rate = -999
            apply_first_pass_is_again = -999
    else:
        apply_first_weekday = -999
        apply_first_time_type = -999
        apply_first_is_holiday = -999
        # 第一次拒绝
        apply_first_refuse_weekday = -999
        apply_first_refuse_time_type = -999
        apply_first_refuse_is_holiday = -999
        apply_current_first_rej_days = -999
        apply_before_first_rej_order_cnt = -999
        apply_before_first_rej_order_pass_cnt = -999
        apply_before_first_rej_order_pass_amt = -999
        apply_after_first_rej_order_cnt = -999
        apply_after_first_rej_order_pass_cnt = -999
        apply_after_first_rej_order_pass_amt = -999
        apply_first_rej_after_before_cnt_rate = -999
        apply_first_rej_after_before_amt_rate = -999
        apply_first_reg_days = -999
        apply_current_first_days = -999
        apply_first_status = -999
        apply_first_amount = -999
        # 第一次放款
        apply_first_pass_weekday = -999
        apply_first_pass_time_type = -999
        apply_first_pass_is_holiday = -999
        apply_current_first_pass_days = -999
        apply_before_first_pass_order_rej_cnt = -999
        apply_after_first_pass_order_rej_cnt = -999
        apply_first_pass_order_first_rej_days = -999
        apply_first_pass_order_first_rej_day_rate = -999
        apply_first_pass_order_last_rej_days = -999
        apply_first_pass_order_last_rej_day_rate = -999
        apply_first_pass_amt = -999
        apply_current_first_amt = -999
        apply_current_first_amt_rate = -999
        apply_first_pass_is_again = -999

    first_features_dict = {
        f'{feature_type}_apply_first_weekday': apply_first_weekday,
        f'{feature_type}_apply_first_time_type': apply_first_time_type,
        f'{feature_type}_apply_first_is_holiday': apply_first_is_holiday,
        f'{feature_type}_apply_first_refuse_weekday': apply_first_refuse_weekday,
        f'{feature_type}_apply_first_refuse_time_type': apply_first_refuse_time_type,
        f'{feature_type}_apply_first_refuse_is_holiday': apply_first_refuse_is_holiday,
        f'{feature_type}_apply_current_first_rej_days': apply_current_first_rej_days,
        f'{feature_type}_apply_before_first_rej_order_cnt': apply_before_first_rej_order_cnt,
        f'{feature_type}_apply_before_first_rej_order_pass_cnt': apply_before_first_rej_order_pass_cnt,
        f'{feature_type}_apply_before_first_rej_order_pass_amt': apply_before_first_rej_order_pass_amt,
        f'{feature_type}_apply_after_first_rej_order_cnt': apply_after_first_rej_order_cnt,
        f'{feature_type}_apply_after_first_rej_order_pass_cnt': apply_after_first_rej_order_pass_cnt,
        f'{feature_type}_apply_after_first_rej_order_pass_amt': apply_after_first_rej_order_pass_amt,
        f'{feature_type}_apply_first_rej_after_before_cnt_rate': apply_first_rej_after_before_cnt_rate,
        f'{feature_type}_apply_first_rej_after_before_amt_rate': apply_first_rej_after_before_amt_rate,
        f'{feature_type}_apply_first_reg_days': apply_first_reg_days,
        f'{feature_type}_apply_current_first_days': apply_current_first_days,
        f'{feature_type}_apply_first_status': apply_first_status,
        f'{feature_type}_apply_first_amount': apply_first_amount,
        f'{feature_type}_apply_first_pass_weekday': apply_first_pass_weekday,
        f'{feature_type}_apply_first_pass_time_type': apply_first_pass_time_type,
        f'{feature_type}_apply_first_pass_is_holiday': apply_first_pass_is_holiday,
        f'{feature_type}_apply_current_first_pass_days': apply_current_first_pass_days,
        f'{feature_type}_apply_before_first_pass_order_rej_cnt': apply_before_first_pass_order_rej_cnt,
        f'{feature_type}_apply_after_first_pass_order_rej_cnt': apply_after_first_pass_order_rej_cnt,
        f'{feature_type}_apply_first_pass_order_first_rej_days': apply_first_pass_order_first_rej_days,
        f'{feature_type}_apply_first_pass_order_first_rej_day_rate': apply_first_pass_order_first_rej_day_rate,
        f'{feature_type}_apply_first_pass_order_last_rej_days': apply_first_pass_order_last_rej_days,
        f'{feature_type}_apply_first_pass_order_last_rej_day_rate': apply_first_pass_order_last_rej_day_rate,
        f'{feature_type}_apply_first_pass_amt': apply_first_pass_amt,
        f'{feature_type}_apply_current_first_amt': apply_current_first_amt,
        f'{feature_type}_apply_current_first_amt_rate': apply_current_first_amt_rate,
        f'{feature_type}_apply_first_pass_is_again': apply_first_pass_is_again,
    }
    return first_features_dict


def get_last_features(user_history_order,
                      feature_type='all',
                      country_id='th',
                      current_loan_amount=None,
                      current_apply_time=None):
    if feature_type == 'all':
        user_history_order = user_history_order.copy()
    elif 'app' in feature_type:
        user_history_order = user_history_order[user_history_order.app_type == feature_type]
    elif 'product' in feature_type:
        user_history_order = user_history_order[user_history_order.product_type == feature_type]

    # 上一笔相关特征
    if len(user_history_order) > 0:
        last_apply_time = user_history_order.iloc[0].apply_time  # 上一笔申请时间
        apply_current_last_days = get_days_inter(current_apply_time, last_apply_time)  # 当前申请时间距离上一笔订单的间隔天数
        apply_last_order_status = 1 if user_history_order.iloc[0].status == 21 else 0  # 上一笔申请是否通过 1 为通过 0 为拒绝
        apply_last_order_amount = user_history_order.iloc[0].loan_amount  # 上一笔申请金额
        apply_current_last_amt = current_loan_amount - apply_last_order_amount  # 当前申请金额与上一笔申请金额的差值 大于0 则为提升的，小于0的为下降
        apply_last_weekday = pd.to_datetime(last_apply_time).weekday()  # 上一笔申请日期是周几
        apply_last_time_type = get_current_time_type(last_apply_time)  # 上一笔申请时间类型晚上 上午 下午 凌晨
        apply_last_is_holiday = get_holidays(country_id=country_id, self_date=last_apply_time)  # 上一笔申请日期是否是节假日

        sub_user_history_order = user_history_order[user_history_order.status == 22]
        if len(sub_user_history_order) > 0:
            last_apply_rej_time = sub_user_history_order.iloc[0].apply_time  # 上一笔拒绝时间
            apply_last_refuse_weekday = pd.to_datetime(last_apply_rej_time).weekday()  # 上一笔拒绝日期是周几
            apply_last_refuse_time_type = get_current_time_type(last_apply_rej_time)  # 上一笔拒绝时间类型晚上 上午 下午 凌晨
            apply_last_refuse_is_holiday = get_holidays(country_id=country_id,self_date=last_apply_rej_time)  # 上一笔拒绝日期是否是节假日
            apply_current_last_rej_days = get_days_inter(current_apply_time,last_apply_rej_time)  # 当前申请时间距离上一笔订拒绝单的间隔天数
            apply_before_last_rej_order_cnt = user_history_order[user_history_order.apply_time < last_apply_rej_time].shape[0]  # 上一笔被拒绝订单之前申请订单次数
            apply_before_last_rej_order_pass_cnt = user_history_order[(user_history_order.apply_time < last_apply_rej_time) & (user_history_order.status == 21)].shape[0]  # 上一笔被拒绝订单之前放款订单次数
            apply_before_last_rej_order_pass_amt = user_history_order[(user_history_order.apply_time < last_apply_rej_time) & (user_history_order.status == 21)].loan_amount.sum()  # 上一笔被拒绝订单之前放款金额和
            apply_after_last_rej_order_cnt = user_history_order[user_history_order.apply_time > last_apply_rej_time].shape[0]  # 上一笔被拒绝订单之后申请订单次数
            apply_after_last_rej_order_pass_cnt = user_history_order[(user_history_order.apply_time > last_apply_rej_time) & (user_history_order.status == 21)].shape[0]  # 上一笔被拒绝订单之后放款订单次数
            apply_after_last_rej_order_pass_amt = user_history_order[(user_history_order.apply_time > last_apply_rej_time) & (user_history_order.status == 21)].loan_amount.sum()  # 上一笔被拒绝订单之后放款金额和
            apply_last_rej_after_before_cnt_rate = fea_div_v2(apply_before_last_rej_order_pass_cnt,apply_after_last_rej_order_pass_cnt)  # 上一笔被拒绝前后的订单放款单量比
            apply_last_rej_after_before_amt_rate = fea_div_v2(apply_before_last_rej_order_pass_amt,apply_after_last_rej_order_pass_amt)  # 上一笔被拒绝订单前后的订单放款金额比
        else:
            apply_last_refuse_weekday = -999
            apply_last_refuse_time_type = -999
            apply_last_refuse_is_holiday = -999
            apply_current_last_rej_days = -999
            apply_before_last_rej_order_cnt = -999
            apply_before_last_rej_order_pass_cnt = -999
            apply_before_last_rej_order_pass_amt = -999
            apply_after_last_rej_order_cnt = -999
            apply_after_last_rej_order_pass_cnt = -999
            apply_after_last_rej_order_pass_amt = -999
            apply_last_rej_after_before_cnt_rate = -999
            apply_last_rej_after_before_amt_rate = -999

        sub_user_history_order = user_history_order[user_history_order.status == 21]
        if len(sub_user_history_order) > 0:
            last_apply_pass_time = sub_user_history_order.iloc[0].apply_time  # 上一笔放款时间
            apply_last_pass_weekday = pd.to_datetime(last_apply_pass_time).weekday()  # 上一笔放款日期是周几
            apply_last_pass_time_type = get_current_time_type(last_apply_pass_time)  # 上一笔放款时间类型晚上 上午 下午 凌晨
            apply_last_pass_is_holiday = get_holidays(country_id=country_id,self_date=last_apply_pass_time)  # 上一笔放款日期是否是节假日

            apply_current_last_pass_days = get_days_inter(current_apply_time,last_apply_pass_time)  # 当前申请时间距离上一笔放款订单的间隔天数
            apply_before_last_pass_order_rej_cnt = user_history_order[(user_history_order.apply_time < last_apply_pass_time) & (user_history_order.status == 22)].shape[0]  # 上一笔放款前被拒绝的订单数量
            apply_after_last_pass_order_rej_cnt = user_history_order[(user_history_order.apply_time > last_apply_pass_time) & (user_history_order.status == 22)].shape[0]  # 上一笔放款前后拒绝的订单数量

            apply_before_last_pass_order_rej_amt_sum = user_history_order[(user_history_order.apply_time < last_apply_pass_time) & (user_history_order.status == 22)].loan_amount.sum()  # 上一笔放款前被拒绝的订单金额和
            apply_before_last_pass_order_rej_amt_max = user_history_order[(user_history_order.apply_time < last_apply_pass_time) & (user_history_order.status == 22)].loan_amount.max()  # 上一笔放款前被拒绝的订单金额最大值
            apply_before_last_pass_order_rej_amt_min = user_history_order[(user_history_order.apply_time < last_apply_pass_time) & (user_history_order.status == 22)].loan_amount.min()  # 上一笔放款前被拒绝的订单金额最小值
            apply_before_last_pass_order_rej_amt_avg = user_history_order[(user_history_order.apply_time < last_apply_pass_time) & (user_history_order.status == 22)].loan_amount.mean()  # 上一笔放款前被拒绝的订单金额平均值
            apply_before_last_pass_order_rej_amt_std = user_history_order[(user_history_order.apply_time < last_apply_pass_time) & (user_history_order.status == 22)].loan_amount.std()  # 上一笔放款前被拒绝的订单金额标准差

            apply_after_last_pass_order_rej_amt_sum = user_history_order[(user_history_order.apply_time > last_apply_pass_time) & (user_history_order.status == 22)].loan_amount.sum()  # 上一笔放款后被拒绝的订单金额和
            apply_after_last_pass_order_rej_amt_max = user_history_order[(user_history_order.apply_time > last_apply_pass_time) & (user_history_order.status == 22)].loan_amount.max()  # 上一笔放款后被拒绝的订单金额最大值
            apply_after_last_pass_order_rej_amt_min = user_history_order[(user_history_order.apply_time > last_apply_pass_time) & (user_history_order.status == 22)].loan_amount.min()  # 上一笔放款后被拒绝的订单金额最小值
            apply_after_last_pass_order_rej_amt_avg = user_history_order[(user_history_order.apply_time > last_apply_pass_time) & (user_history_order.status == 22)].loan_amount.mean()  # 上一笔放款后被拒绝的订单金额平均值
            apply_after_last_pass_order_rej_amt_std = user_history_order[(user_history_order.apply_time > last_apply_pass_time) & (user_history_order.status == 22)].loan_amount.std()  # 上一笔放款后被拒绝的订单金额标准差

            tmp_user_history_order = user_history_order[(user_history_order.apply_time > last_apply_pass_time) & (user_history_order.status == 22)]
            if len(tmp_user_history_order) > 0:
                last_first_refuse_time_sub = tmp_user_history_order.iloc[-1].apply_time  # 上一笔放款后，第一次拒绝时间
                last_last_refuse_time_sub = tmp_user_history_order.iloc[0].apply_time  # 上一笔放款后，最近一次拒绝时间
                apply_first_pass_order_last_rej_days = get_days_inter(last_first_refuse_time_sub,last_apply_pass_time)  # 上一笔放款后，第一笔拒绝订单距离上一笔放款时间间隔
                apply_first_pass_order_last_rej_day_rate = fea_div_v2(apply_first_pass_order_last_rej_days,apply_current_last_pass_days)  # 上一笔放款后，第一笔拒绝订单距离上一笔放款时间间隔 / 本次申请时间差 - 上一笔放款时间
                apply_last_pass_order_last_rej_days = get_days_inter(last_last_refuse_time_sub,last_apply_pass_time)  # 上一笔放款后，最近一次拒绝订单距离上一笔放款时间间隔
                apply_last_pass_order_last_rej_day_rate = fea_div_v2(apply_last_pass_order_last_rej_days,apply_current_last_pass_days)  # 上一笔放款后，最近一次拒绝订单 距离时间差 / 本次申请时间差 - 首次放款时间
            else:
                apply_first_pass_order_last_rej_days = -999
                apply_first_pass_order_last_rej_day_rate = -999
                apply_last_pass_order_last_rej_days = -999
                apply_last_pass_order_last_rej_day_rate = -999

            last_pass_order_id = user_history_order[(user_history_order.status == 21) & (user_history_order.apply_time == last_apply_pass_time)].iloc[0].app_order_id
            apply_last_pass_amt = user_history_order[user_history_order.app_order_id == last_pass_order_id].loan_amount.values[0]  # 上一笔放款金额
            apply_current_last_pass_amt = current_loan_amount - apply_last_pass_amt  # 本次订单金额  - 上一笔放款金额
            apply_current_last_pass_amt_rate = fea_div_v2(apply_current_last_pass_amt,apply_last_pass_amt)  # 本次订单金额 - 第一笔放款金额/ 第一笔放款金额

            last_pass_detail_df = user_history_order[user_history_order.apply_time < last_apply_pass_time].copy()
            apply_last_pass_is_again = if_first_order_new(last_pass_detail_df)  # 上一笔放款是首单还是再次单首次放款的话 全部为首单或者第一单，如果是再次单，则说明计算错误
        else:
            apply_last_pass_weekday = -999
            apply_last_pass_time_type = -999
            apply_last_pass_is_holiday = -999
            apply_current_last_pass_days = -999
            apply_before_last_pass_order_rej_cnt = -999
            apply_after_last_pass_order_rej_cnt = -999
            apply_before_last_pass_order_rej_amt_sum = -999
            apply_before_last_pass_order_rej_amt_max = -999
            apply_before_last_pass_order_rej_amt_min = -999
            apply_before_last_pass_order_rej_amt_avg = -999
            apply_before_last_pass_order_rej_amt_std = -999
            apply_after_last_pass_order_rej_amt_sum = -999
            apply_after_last_pass_order_rej_amt_max = -999
            apply_after_last_pass_order_rej_amt_min = -999
            apply_after_last_pass_order_rej_amt_avg = -999
            apply_after_last_pass_order_rej_amt_std = -999
            apply_first_pass_order_last_rej_days = -999
            apply_first_pass_order_last_rej_day_rate = -999
            apply_last_pass_order_last_rej_days = -999
            apply_last_pass_order_last_rej_day_rate = -999
            apply_last_pass_amt = -999
            apply_current_last_pass_amt = -999
            apply_current_last_pass_amt_rate = -999
            apply_last_pass_is_again = -999
    else:
        apply_last_pass_weekday = -999
        apply_last_pass_time_type = -999
        apply_last_pass_is_holiday = -999
        apply_current_last_pass_days = -999
        apply_before_last_pass_order_rej_cnt = -999
        apply_after_last_pass_order_rej_cnt = -999
        apply_before_last_pass_order_rej_amt_sum = -999
        apply_before_last_pass_order_rej_amt_max = -999
        apply_before_last_pass_order_rej_amt_min = -999
        apply_before_last_pass_order_rej_amt_avg = -999
        apply_before_last_pass_order_rej_amt_std = -999
        apply_after_last_pass_order_rej_amt_sum = -999
        apply_after_last_pass_order_rej_amt_max = -999
        apply_after_last_pass_order_rej_amt_min = -999
        apply_after_last_pass_order_rej_amt_avg = -999
        apply_after_last_pass_order_rej_amt_std = -999
        apply_first_pass_order_last_rej_days = -999
        apply_first_pass_order_last_rej_day_rate = -999
        apply_last_pass_order_last_rej_days = -999
        apply_last_pass_order_last_rej_day_rate = -999
        apply_last_pass_amt = -999
        apply_current_last_pass_amt = -999
        apply_last_pass_is_again = -999
        apply_last_refuse_weekday = -999
        apply_last_refuse_time_type = -999
        apply_last_refuse_is_holiday = -999
        apply_current_last_rej_days = -999
        apply_before_last_rej_order_cnt = -999
        apply_before_last_rej_order_pass_cnt = -999
        apply_before_last_rej_order_pass_amt = -999
        apply_after_last_rej_order_cnt = -999
        apply_after_last_rej_order_pass_cnt = -999
        apply_after_last_rej_order_pass_amt = -999
        apply_last_rej_after_before_cnt_rate = -999
        apply_last_rej_after_before_amt_rate = -999
        apply_current_last_days = -999
        apply_last_order_status = -999
        apply_last_order_amount = -999
        apply_current_last_amt = -999
        apply_last_weekday = -999
        apply_last_time_type = -999
        apply_last_is_holiday = -999
        apply_current_last_pass_amt_rate = -999

    last_feature_dict = {f'{feature_type}_apply_current_last_days': apply_current_last_days,
                         f'{feature_type}_apply_last_order_status': apply_last_order_status,
                         f'{feature_type}_apply_last_order_amount': apply_last_order_amount,
                         f'{feature_type}_apply_current_last_amt': apply_current_last_amt,
                         f'{feature_type}_apply_last_weekday': apply_last_weekday,
                         f'{feature_type}_apply_last_time_type': apply_last_time_type,
                         f'{feature_type}_apply_last_is_holiday': apply_last_is_holiday,
                         f'{feature_type}_apply_last_refuse_weekday': apply_last_refuse_weekday,
                         f'{feature_type}_apply_last_refuse_time_type': apply_last_refuse_time_type,
                         f'{feature_type}_apply_last_refuse_is_holiday': apply_last_refuse_is_holiday,
                         f'{feature_type}_apply_current_last_rej_days': apply_current_last_rej_days,
                         f'{feature_type}_apply_before_last_rej_order_cnt': apply_before_last_rej_order_cnt,
                         f'{feature_type}_apply_before_last_rej_order_pass_cnt': apply_before_last_rej_order_pass_cnt,
                         f'{feature_type}_apply_before_last_rej_order_pass_amt': apply_before_last_rej_order_pass_amt,
                         f'{feature_type}_apply_after_last_rej_order_cnt': apply_after_last_rej_order_cnt,
                         f'{feature_type}_apply_after_last_rej_order_pass_cnt': apply_after_last_rej_order_pass_cnt,
                         f'{feature_type}_apply_after_last_rej_order_pass_amt': apply_after_last_rej_order_pass_amt,
                         f'{feature_type}_apply_last_rej_after_before_cnt_rate': apply_last_rej_after_before_cnt_rate,
                         f'{feature_type}_apply_last_rej_after_before_amt_rate': apply_last_rej_after_before_amt_rate,
                         f'{feature_type}_apply_last_pass_weekday': apply_last_pass_weekday,
                         f'{feature_type}_apply_last_pass_time_type': apply_last_pass_time_type,
                         f'{feature_type}_apply_last_pass_is_holiday': apply_last_pass_is_holiday,
                         f'{feature_type}_apply_current_last_pass_days': apply_current_last_pass_days,
                         f'{feature_type}_apply_before_last_pass_order_rej_cnt': apply_before_last_pass_order_rej_cnt,
                         f'{feature_type}_apply_after_last_pass_order_rej_cnt': apply_after_last_pass_order_rej_cnt,
                         f'{feature_type}_apply_before_last_pass_order_rej_amt_sum': apply_before_last_pass_order_rej_amt_sum,
                         f'{feature_type}_apply_before_last_pass_order_rej_amt_max': apply_before_last_pass_order_rej_amt_max,
                         f'{feature_type}_apply_before_last_pass_order_rej_amt_min': apply_before_last_pass_order_rej_amt_min,
                         f'{feature_type}_apply_before_last_pass_order_rej_amt_avg': apply_before_last_pass_order_rej_amt_avg,
                         f'{feature_type}_apply_before_last_pass_order_rej_amt_std': apply_before_last_pass_order_rej_amt_std,
                         f'{feature_type}_apply_after_last_pass_order_rej_amt_sum': apply_after_last_pass_order_rej_amt_sum,
                         f'{feature_type}_apply_after_last_pass_order_rej_amt_max': apply_after_last_pass_order_rej_amt_max,
                         f'{feature_type}_apply_after_last_pass_order_rej_amt_min': apply_after_last_pass_order_rej_amt_min,
                         f'{feature_type}_apply_after_last_pass_order_rej_amt_avg': apply_after_last_pass_order_rej_amt_avg,
                         f'{feature_type}_apply_after_last_pass_order_rej_amt_std': apply_after_last_pass_order_rej_amt_std,
                         f'{feature_type}_apply_first_pass_order_last_rej_days': apply_first_pass_order_last_rej_days,
                         f'{feature_type}_apply_first_pass_order_last_rej_day_rate': apply_first_pass_order_last_rej_day_rate,
                         f'{feature_type}_apply_last_pass_order_last_rej_days': apply_last_pass_order_last_rej_days,
                         f'{feature_type}_apply_last_pass_order_last_rej_day_rate': apply_last_pass_order_last_rej_day_rate,
                         f'{feature_type}_apply_last_pass_amt': apply_last_pass_amt,
                         f'{feature_type}_apply_current_last_pass_amt': apply_current_last_pass_amt,
                         f'{feature_type}_apply_current_last_pass_amt_rate': apply_current_last_pass_amt_rate,
                         f'{feature_type}_apply_last_pass_is_again': apply_last_pass_is_again, }
    return last_feature_dict


def get_order_sta_features(user_history_order,
                           day_list=[1, 3, 7, 15, 30, 60, 90, 180, 'all'],
                           feature_type='all',
                           country_id='th'):
    order_sta_feature_dict = {}

    if feature_type == 'all':
        user_history_order = user_history_order.copy()
    elif 'app' in feature_type:
        user_history_order = user_history_order[user_history_order.app_type == feature_type]
    elif 'product' in feature_type:
        user_history_order = user_history_order[user_history_order.product_type == feature_type]

    for day in day_list:
        if day == 'all':
            day_sub_df = user_history_order.copy()  # 最近N天的申请数据
        else:
            day_sub_df = user_history_order[user_history_order.order_day_inter <= day].copy()  # 最近N天的申请数据
        apply_order_cnt = len(day_sub_df)  # 申请借款总次数
        apply_rej_order_cnt = len(day_sub_df[day_sub_df.status == 22])  # 申请拒绝借款总次数
        apply_pass_order_cnt = len(day_sub_df[day_sub_df.status == 21])  # 申请通过借款总次数
        apply_order_amt = day_sub_df.loan_amount.sum()  # 申请借款总金额
        apply_rej_order_amt_sum = day_sub_df[day_sub_df.status == 22].loan_amount.sum()  # 申请拒绝借款总金额和
        apply_rej_order_amt_max = day_sub_df[day_sub_df.status == 22].loan_amount.max()  # 申请拒绝借款总金额最大值
        apply_rej_order_amt_min = day_sub_df[day_sub_df.status == 22].loan_amount.min()  # 申请拒绝借款总金额最小值
        apply_rej_order_amt_avg = day_sub_df[day_sub_df.status == 22].loan_amount.mean()  # 申请拒绝借款总金额平均值
        apply_rej_order_amt_std = day_sub_df[day_sub_df.status == 22].loan_amount.std()  # 申请拒绝借款总金额标准差

        apply_pass_order_amt_sum = day_sub_df[day_sub_df.status == 21].loan_amount.sum()  # 申请通过借款总金额和
        apply_pass_order_amt_max = day_sub_df[day_sub_df.status == 21].loan_amount.max()  # 申请通过借款总金额最大值
        apply_pass_order_amt_min = day_sub_df[day_sub_df.status == 21].loan_amount.min()  # 申请通过借款总金额最小值
        apply_pass_order_amt_avg = day_sub_df[day_sub_df.status == 21].loan_amount.mean()  # 申请通过借款总金额平均值
        apply_pass_order_amt_std = day_sub_df[day_sub_df.status == 21].loan_amount.std()  # 申请通过借款总金额标准差

        apply_rej_order_cnt_rate = fea_div_v2(apply_rej_order_cnt, apply_order_cnt)  # 申请放款拒绝率
        apply_pass_order_cnt_rate = fea_div_v2(apply_pass_order_cnt, apply_order_cnt)  # 申请放款成功率
        apply_rej_order_amt_rate = fea_div_v2(apply_rej_order_amt_sum, apply_order_amt)  # 申请放款拒绝金额占比
        apply_pass_order_amt_rate = fea_div_v2(apply_pass_order_amt_sum, apply_order_amt)  # 申请放款成功金额占比

        apply_app_cnt = day_sub_df.acq_channel.nunique()  # 申请的app个数
        apply_app_pass_cnt = day_sub_df[day_sub_df.status == 21].acq_channel.nunique()  # 申请成功的app个数
        apply_app_rej_cnt = day_sub_df[day_sub_df.status == 22].acq_channel.nunique()  # 申请拒绝的app个数

        apply_product_cnt = day_sub_df.product_code.nunique()  # 申请的product个数
        apply_product_pass_cnt = day_sub_df[day_sub_df.status == 21].product_code.nunique()  # 申请成功的product个数
        apply_product_rej_cnt = day_sub_df[day_sub_df.status == 22].product_code.nunique()  # 申请拒绝的product个数

        # 当前订单是否是首单 1 是首单 2 是再次单 0 是在当前产品是时间首单, 默认是首单 , 根据不同数据进行修改
        tmp1_day_sub_df = day_sub_df
        tmp1_day_sub_df['last_status'] = tmp1_day_sub_df['status'].shift(-1)
        tmp1_day_sub_df['apply_if_again'] = tmp1_day_sub_df['last_status'].apply(if_first_order)

        apply_order_type_first_cnt = len(tmp1_day_sub_df[tmp1_day_sub_df['apply_if_again'] == 1])  # 申请订单的首次订单总数次数
        apply_order_type_again_cnt = len(tmp1_day_sub_df[tmp1_day_sub_df['apply_if_again'] == 2])  # 申请订单的再次订单总数次数
        apply_order_type_first_rate = fea_div_v2(apply_order_type_first_cnt, apply_order_cnt)  # 申请订单的首次订单总数占比
        apply_order_type_again_rate = fea_div_v2(apply_order_type_again_cnt, apply_order_cnt)  # 申请订单的再次订单总数占比

        # 当前订单是否是首单 1 是首单 2 是再次单 0 是在当前产品是时间首单, 默认是首单 , 根据不同数据进行修改
        tmp2_day_sub_df = day_sub_df[day_sub_df.status == 21]
        tmp2_day_sub_df['last_status'] = tmp2_day_sub_df['status'].shift(-1)
        tmp2_day_sub_df['pass_if_again'] = tmp2_day_sub_df['last_status'].apply(if_first_order)

        pass_order_type_first_cnt = len(tmp2_day_sub_df[tmp2_day_sub_df['pass_if_again'] == 1])  # 放款订单的首次订单总数次数
        pass_order_type_again_cnt = len(tmp2_day_sub_df[tmp2_day_sub_df['pass_if_again'] == 2])  # 放款订单的再次订单总数次数
        pass_order_type_first_rate = fea_div_v2(pass_order_type_first_cnt, apply_order_cnt)  # 放款订单的首次订单总数占比
        pass_order_type_again_rate = fea_div_v2(pass_order_type_again_cnt, apply_order_cnt)  # 放款订单的再次订单总数占比

        # 区分工作日和休息日week_day.isin([6,7])休息日 。time_type 1 上午 2 下午 3 晚上 4 凌晨， is_holiday， 1,0，
        week_day_df = day_sub_df[day_sub_df.week_day.isin([6, 7])]
        apply_week_day_cnt = len(week_day_df)  # 周末申请订单数
        apply_week_rej_order_cnt = len(week_day_df[week_day_df.status == 22])  # 周末申请拒绝借款总次数
        apply_week_pass_order_cnt = len(week_day_df[week_day_df.status == 21])  # 周末申请通过借款总次数
        apply_week_order_amt = week_day_df.loan_amount.sum()  # 周末申请借款总金额
        apply_week_rej_order_amt_sum = week_day_df[week_day_df.status == 22].loan_amount.sum()  # 周末申请拒绝借款总金额和
        apply_week_pass_order_amt_sum = week_day_df[week_day_df.status == 21].loan_amount.sum()  # 周末申请通过借款总金额和
        apply_week_day_cnt_rate = fea_div_v2(apply_week_day_cnt, apply_order_cnt)  # 周末申请的订单数占总订单数的比例
        apply_week_rej_order_cnt_rate = fea_div_v2(apply_week_rej_order_cnt,apply_rej_order_cnt)  # 周末申请的拒绝订单数占总拒绝订单数的比例
        apply_week_pass_order_cnt_rate = fea_div_v2(apply_week_pass_order_cnt,apply_pass_order_cnt)  # 周末申请的通过订单数占总拒绝订单数的比例
        apply_week_order_amt_rate = fea_div_v2(apply_week_order_amt, apply_order_amt)  # 周末申请的订单金额占总订单金额的比例
        apply_week_rej_order_amt_rate = fea_div_v2(apply_week_rej_order_amt_sum,apply_rej_order_amt_sum)  # 周末申请的拒绝订单金额占总订单拒绝金额的比例
        apply_week_pass_order_amt_rate = fea_div_v2(apply_week_pass_order_amt_sum,apply_pass_order_amt_sum)  # 周末申请的通过订单金额占总订单通过金额的比例

        work_day_df = day_sub_df[~day_sub_df.week_day.isin([6, 7])]
        apply_work_day_cnt = len(work_day_df)  # 工作日申请订单数
        apply_work_rej_order_cnt = len(work_day_df[work_day_df.status == 22])  # 工作日申请拒绝借款总次数
        apply_work_pass_order_cnt = len(work_day_df[work_day_df.status == 21])  # 工作日申请通过借款总次数
        apply_work_order_amt = work_day_df.loan_amount.sum()  # 工作日申请借款总金额
        apply_work_rej_order_amt_sum = work_day_df[work_day_df.status == 22].loan_amount.sum()  # 工作日申请拒绝借款总金额和
        apply_work_pass_order_amt_sum = work_day_df[work_day_df.status == 21].loan_amount.sum()  # 工作日申请通过借款总金额和
        apply_work_day_cnt_rate = fea_div_v2(apply_work_day_cnt, apply_order_cnt)  # 工作日申请的订单数占总订单数的比例
        apply_work_rej_order_cnt_rate = fea_div_v2(apply_work_rej_order_cnt,apply_rej_order_cnt)  # 工作日申请的拒绝订单数占总拒绝订单数的比例
        apply_work_pass_order_cnt_rate = fea_div_v2(apply_work_pass_order_cnt,apply_pass_order_cnt)  # 工作日申请的通过订单数占总拒绝订单数的比例
        apply_work_order_amt_rate = fea_div_v2(apply_work_order_amt, apply_order_amt)  # 工作日申请的订单金额占总订单金额的比例
        apply_work_rej_order_amt_rate = fea_div_v2(apply_work_rej_order_amt_sum,apply_rej_order_amt_sum)  # 工作日申请的拒绝订单金额占总订单拒绝金额的比例
        apply_work_pass_order_amt_rate = fea_div_v2(apply_work_pass_order_amt_sum,apply_pass_order_amt_sum)  # 工作日申请的通过订单金额占总订单通过金额的比例

        order_sta_feature_dict[f'{feature_type}_apply_order_cnt_{day}d'] = apply_order_cnt
        order_sta_feature_dict[f'{feature_type}_apply_rej_order_cnt_{day}d'] = apply_rej_order_cnt
        order_sta_feature_dict[f'{feature_type}_apply_pass_order_cnt_{day}d'] = apply_pass_order_cnt
        order_sta_feature_dict[f'{feature_type}_apply_order_amt_{day}d'] = apply_order_amt
        order_sta_feature_dict[f'{feature_type}_apply_rej_order_amt_sum_{day}d'] = apply_rej_order_amt_sum
        order_sta_feature_dict[f'{feature_type}_apply_rej_order_amt_max_{day}d'] = apply_rej_order_amt_max
        order_sta_feature_dict[f'{feature_type}_apply_rej_order_amt_min_{day}d'] = apply_rej_order_amt_min
        order_sta_feature_dict[f'{feature_type}_apply_rej_order_amt_avg_{day}d'] = apply_rej_order_amt_avg
        order_sta_feature_dict[f'{feature_type}_apply_rej_order_amt_std_{day}d'] = apply_rej_order_amt_std
        order_sta_feature_dict[f'{feature_type}_apply_pass_order_amt_sum_{day}d'] = apply_pass_order_amt_sum
        order_sta_feature_dict[f'{feature_type}_apply_pass_order_amt_max_{day}d'] = apply_pass_order_amt_max
        order_sta_feature_dict[f'{feature_type}_apply_pass_order_amt_min_{day}d'] = apply_pass_order_amt_min
        order_sta_feature_dict[f'{feature_type}_apply_pass_order_amt_avg_{day}d'] = apply_pass_order_amt_avg
        order_sta_feature_dict[f'{feature_type}_apply_pass_order_amt_std_{day}d'] = apply_pass_order_amt_std
        order_sta_feature_dict[f'{feature_type}_apply_rej_order_cnt_rate_{day}d'] = apply_rej_order_cnt_rate
        order_sta_feature_dict[f'{feature_type}_apply_pass_order_cnt_rate_{day}d'] = apply_pass_order_cnt_rate
        order_sta_feature_dict[f'{feature_type}_apply_rej_order_amt_rate_{day}d'] = apply_rej_order_amt_rate
        order_sta_feature_dict[f'{feature_type}_apply_pass_order_amt_rate_{day}d'] = apply_pass_order_amt_rate
        order_sta_feature_dict[f'{feature_type}_apply_app_cnt_{day}d'] = apply_app_cnt
        order_sta_feature_dict[f'{feature_type}_apply_app_pass_cnt_{day}d'] = apply_app_pass_cnt
        order_sta_feature_dict[f'{feature_type}_apply_app_rej_cnt_{day}d'] = apply_app_rej_cnt
        order_sta_feature_dict[f'{feature_type}_apply_product_cnt_{day}d'] = apply_product_cnt
        order_sta_feature_dict[f'{feature_type}_apply_product_pass_cnt_{day}d'] = apply_product_pass_cnt
        order_sta_feature_dict[f'{feature_type}_apply_product_rej_cnt_{day}d'] = apply_product_rej_cnt
        order_sta_feature_dict[f'{feature_type}_apply_order_type_first_cnt_{day}d'] = apply_order_type_first_cnt
        order_sta_feature_dict[f'{feature_type}_apply_order_type_again_cnt_{day}d'] = apply_order_type_again_cnt
        order_sta_feature_dict[f'{feature_type}_apply_order_type_first_rate_{day}d'] = apply_order_type_first_rate
        order_sta_feature_dict[f'{feature_type}_apply_order_type_again_rate_{day}d'] = apply_order_type_again_rate
        order_sta_feature_dict[f'{feature_type}_pass_order_type_first_cnt_{day}d'] = pass_order_type_first_cnt
        order_sta_feature_dict[f'{feature_type}_pass_order_type_again_cnt_{day}d'] = pass_order_type_again_cnt
        order_sta_feature_dict[f'{feature_type}_pass_order_type_first_rate_{day}d'] = pass_order_type_first_rate
        order_sta_feature_dict[f'{feature_type}_pass_order_type_again_rate_{day}d'] = pass_order_type_again_rate
        order_sta_feature_dict[f'{feature_type}_apply_week_day_cnt_{day}d'] = apply_week_day_cnt
        order_sta_feature_dict[f'{feature_type}_apply_week_rej_order_cnt_{day}d'] = apply_week_rej_order_cnt
        order_sta_feature_dict[f'{feature_type}_apply_week_pass_order_cnt_{day}d'] = apply_week_pass_order_cnt
        order_sta_feature_dict[f'{feature_type}_apply_week_order_amt_{day}d'] = apply_week_order_amt
        order_sta_feature_dict[f'{feature_type}_apply_week_rej_order_amt_sum_{day}d'] = apply_week_rej_order_amt_sum
        order_sta_feature_dict[f'{feature_type}_apply_week_pass_order_amt_sum_{day}d'] = apply_week_pass_order_amt_sum
        order_sta_feature_dict[f'{feature_type}_apply_week_day_cnt_rate_{day}d'] = apply_week_day_cnt_rate
        order_sta_feature_dict[f'{feature_type}_apply_week_rej_order_cnt_rate_{day}d'] = apply_week_rej_order_cnt_rate
        order_sta_feature_dict[f'{feature_type}_apply_week_pass_order_cnt_rate_{day}d'] = apply_week_pass_order_cnt_rate
        order_sta_feature_dict[f'{feature_type}_apply_week_order_amt_rate_{day}d'] = apply_week_order_amt_rate
        order_sta_feature_dict[f'{feature_type}_apply_week_rej_order_amt_rate_{day}d'] = apply_week_rej_order_amt_rate
        order_sta_feature_dict[f'{feature_type}_apply_week_pass_order_amt_rate_{day}d'] = apply_week_pass_order_amt_rate
        order_sta_feature_dict[f'{feature_type}_apply_work_day_cnt_{day}d'] = apply_work_day_cnt
        order_sta_feature_dict[f'{feature_type}_apply_work_rej_order_cnt_{day}d'] = apply_work_rej_order_cnt
        order_sta_feature_dict[f'{feature_type}_apply_work_pass_order_cnt_{day}d'] = apply_work_pass_order_cnt
        order_sta_feature_dict[f'{feature_type}_apply_work_order_amt_{day}d'] = apply_work_order_amt
        order_sta_feature_dict[f'{feature_type}_apply_work_rej_order_amt_sum_{day}d'] = apply_work_rej_order_amt_sum
        order_sta_feature_dict[f'{feature_type}_apply_work_pass_order_amt_sum_{day}d'] = apply_work_pass_order_amt_sum
        order_sta_feature_dict[f'{feature_type}_apply_work_day_cnt_rate_{day}d'] = apply_work_day_cnt_rate
        order_sta_feature_dict[f'{feature_type}_apply_work_rej_order_cnt_rate_{day}d'] = apply_work_rej_order_cnt_rate
        order_sta_feature_dict[f'{feature_type}_apply_work_pass_order_cnt_rate_{day}d'] = apply_work_pass_order_cnt_rate
        order_sta_feature_dict[f'{feature_type}_apply_work_order_amt_rate_{day}d'] = apply_work_order_amt_rate
        order_sta_feature_dict[f'{feature_type}_apply_work_rej_order_amt_rate_{day}d'] = apply_work_rej_order_amt_rate
        order_sta_feature_dict[f'{feature_type}_apply_work_pass_order_amt_rate_{day}d'] = apply_work_pass_order_amt_rate


        for tp in [1, 2, 3, 4]:
            tp_day_df = day_sub_df[day_sub_df.time_type == tp]
            apply_tp_day_cnt = len(tp_day_df)  # 某个时间段申请订单数
            apply_tp_rej_order_cnt = len(tp_day_df[tp_day_df.status == 22])  # 某个时间段申请拒绝借款总次数
            apply_tp_pass_order_cnt = len(tp_day_df[tp_day_df.status == 21])  # 某个时间段申请通过借款总次数
            apply_tp_order_amt = tp_day_df.loan_amount.sum()  # 某个时间段申请借款总金额
            apply_tp_rej_order_amt_sum = tp_day_df[tp_day_df.status == 22].loan_amount.sum()  # 某个时间段申请拒绝借款总金额和
            apply_tp_pass_order_amt_sum = tp_day_df[tp_day_df.status == 21].loan_amount.sum()  # 某个时间段申请通过借款总金额和
            apply_tp_day_cnt_rate = fea_div_v2(apply_tp_day_cnt, apply_order_cnt)  # 某个时间段申请的订单数占总订单数的比例
            apply_tp_rej_order_cnt_rate = fea_div_v2(apply_tp_rej_order_cnt,apply_rej_order_cnt)  # 某个时间段申请的拒绝订单数占总拒绝订单数的比例
            apply_tp_pass_order_cnt_rate = fea_div_v2(apply_tp_pass_order_cnt,apply_pass_order_cnt)  # 某个时间段申请的通过订单数占总拒绝订单数的比例
            apply_tp_order_amt_rate = fea_div_v2(apply_tp_order_amt, apply_order_amt)  # 某个时间段申请的订单金额占总订单金额的比例
            apply_tp_rej_order_amt_rate = fea_div_v2(apply_tp_rej_order_amt_sum,apply_rej_order_amt_sum)  # 某个时间段申请的拒绝订单金额占总订单拒绝金额的比例
            apply_tp_pass_order_amt_rate = fea_div_v2(apply_tp_pass_order_amt_sum,apply_pass_order_amt_sum)  # 某个时间段申请的通过订单金额占总订单通过金额的比例

            order_sta_feature_dict[f'{feature_type}_apply_tp{tp}_day_cnt_{day}d'] = apply_tp_day_cnt
            order_sta_feature_dict[f'{feature_type}_apply_tp{tp}_rej_order_cnt_{day}d'] = apply_tp_rej_order_cnt
            order_sta_feature_dict[f'{feature_type}_apply_tp{tp}_pass_order_cnt_{day}d'] = apply_tp_pass_order_cnt
            order_sta_feature_dict[f'{feature_type}_apply_tp{tp}_order_amt_{day}d'] = apply_tp_order_amt
            order_sta_feature_dict[f'{feature_type}_apply_tp{tp}_rej_order_amt_sum_{day}d'] = apply_tp_rej_order_amt_sum
            order_sta_feature_dict[f'{feature_type}_apply_tp{tp}_pass_order_amt_sum_{day}d'] = apply_tp_pass_order_amt_sum
            order_sta_feature_dict[f'{feature_type}_apply_tp{tp}_day_cnt_rate_{day}d'] = apply_tp_day_cnt_rate
            order_sta_feature_dict[f'{feature_type}_apply_tp{tp}_rej_order_cnt_rate_{day}d'] = apply_tp_rej_order_cnt_rate
            order_sta_feature_dict[f'{feature_type}_apply_tp{tp}_pass_order_cnt_rate_{day}d'] = apply_tp_pass_order_cnt_rate
            order_sta_feature_dict[f'{feature_type}_apply_tp{tp}_order_amt_rate_{day}d'] = apply_tp_order_amt_rate
            order_sta_feature_dict[f'{feature_type}_apply_tp{tp}_rej_order_amt_rate_{day}d'] = apply_tp_rej_order_amt_rate
            order_sta_feature_dict[f'{feature_type}_apply_tp{tp}_pass_order_amt_rate_{day}d'] = apply_tp_pass_order_amt_rate

        for hy in [0, 1]:
            hy_day_df = day_sub_df[day_sub_df.is_holiday == hy]
            apply_hy_day_cnt = len(hy_day_df)  # 节假日时间段申请订单数
            apply_hy_rej_order_cnt = len(hy_day_df[hy_day_df.status == 22])  # 节假日时间段申请拒绝借款总次数
            apply_hy_pass_order_cnt = len(hy_day_df[hy_day_df.status == 21])  # 节假日时间段申请通过借款总次数
            apply_hy_order_amt = hy_day_df.loan_amount.sum()  # 节假日时间段申请借款总金额
            apply_hy_rej_order_amt_sum = hy_day_df[hy_day_df.status == 22].loan_amount.sum()  # 节假日时间段申请拒绝借款总金额和
            apply_hy_pass_order_amt_sum = hy_day_df[hy_day_df.status == 21].loan_amount.sum()  # 节假日时间段申请通过借款总金额和
            apply_hy_day_cnt_rate = fea_div_v2(apply_hy_day_cnt, apply_order_cnt)  # 节假日时间段申请的订单数占总订单数的比例
            apply_hy_rej_order_cnt_rate = fea_div_v2(apply_hy_rej_order_cnt,apply_rej_order_cnt)  # 节假日时间段申请的拒绝订单数占总拒绝订单数的比例
            apply_hy_pass_order_cnt_rate = fea_div_v2(apply_hy_pass_order_cnt,apply_pass_order_cnt)  # 节假日时间段申请的通过订单数占总拒绝订单数的比例
            apply_hy_order_amt_rate = fea_div_v2(apply_hy_order_amt, apply_order_amt)  # 节假日时间段申请的订单金额占总订单金额的比例
            apply_hy_rej_order_amt_rate = fea_div_v2(apply_hy_rej_order_amt_sum,apply_rej_order_amt_sum)  # 节假日时间段申请的拒绝订单金额占总订单拒绝金额的比例
            apply_hy_pass_order_amt_rate = fea_div_v2(apply_hy_pass_order_amt_sum,apply_pass_order_amt_sum)  # 节假日时间段申请的通过订单金额占总订单通过金额的比例

            order_sta_feature_dict[f'{feature_type}_apply_hy{hy}_day_cnt_{day}d'] = apply_hy_day_cnt
            order_sta_feature_dict[f'{feature_type}_apply_hy{hy}_rej_order_cnt_{day}d'] = apply_hy_rej_order_cnt
            order_sta_feature_dict[f'{feature_type}_apply_hy{hy}_pass_order_cnt_{day}d'] = apply_hy_pass_order_cnt
            order_sta_feature_dict[f'{feature_type}_apply_hy{hy}_order_amt_{day}d'] = apply_hy_order_amt
            order_sta_feature_dict[f'{feature_type}_apply_hy{hy}_rej_order_amt_sum_{day}d'] = apply_hy_rej_order_amt_sum
            order_sta_feature_dict[f'{feature_type}_apply_hy{hy}_pass_order_amt_sum_{day}d'] = apply_hy_pass_order_amt_sum
            order_sta_feature_dict[f'{feature_type}_apply_hy{hy}_day_cnt_rate_{day}d'] = apply_hy_day_cnt_rate
            order_sta_feature_dict[f'{feature_type}_apply_hy{hy}_rej_order_cnt_rate_{day}d'] = apply_hy_rej_order_cnt_rate
            order_sta_feature_dict[f'{feature_type}_apply_hy{hy}_pass_order_cnt_rate_{day}d'] = apply_hy_pass_order_cnt_rate
            order_sta_feature_dict[f'{feature_type}_apply_hy{hy}_order_amt_rate_{day}d'] = apply_hy_order_amt_rate
            order_sta_feature_dict[f'{feature_type}_apply_hy{hy}_rej_order_amt_rate_{day}d'] = apply_hy_rej_order_amt_rate
            order_sta_feature_dict[f'{feature_type}_apply_hy{hy}_pass_order_amt_rate_{day}d'] = apply_hy_pass_order_amt_rate

    return order_sta_feature_dict


def get_order_rd_features(user_history_order,
                          records_list=[1, 3, 7, 15, 30],
                          feature_type='all',
                          country_id='th'):
    order_rd_feature_dict = {}

    if feature_type == 'all':
        user_history_order = user_history_order.copy()
    elif 'app' in feature_type:
        user_history_order = user_history_order[user_history_order.app_type == feature_type]
    elif 'product' in feature_type:
        user_history_order = user_history_order[user_history_order.product_type == feature_type]

    for rd in records_list:
        rd_sub_df = user_history_order.iloc[:rd].copy()  # 最近N笔的申请数据
        apply_order_cnt = len(rd_sub_df)  # 申请借款总次数
        apply_rej_order_cnt = len(rd_sub_df[rd_sub_df.status == 22])  # 申请拒绝借款总次数
        apply_pass_order_cnt = len(rd_sub_df[rd_sub_df.status == 21])  # 申请通过借款总次数
        apply_order_amt = rd_sub_df.loan_amount.sum()  # 申请借款总金额
        apply_rej_order_amt_sum = rd_sub_df[rd_sub_df.status == 22].loan_amount.sum()  # 申请拒绝借款总金额和
        apply_rej_order_amt_max = rd_sub_df[rd_sub_df.status == 22].loan_amount.max()  # 申请拒绝借款总金额最大值
        apply_rej_order_amt_min = rd_sub_df[rd_sub_df.status == 22].loan_amount.min()  # 申请拒绝借款总金额最小值
        apply_rej_order_amt_avg = rd_sub_df[rd_sub_df.status == 22].loan_amount.mean()  # 申请拒绝借款总金额平均值
        apply_rej_order_amt_std = rd_sub_df[rd_sub_df.status == 22].loan_amount.std()  # 申请拒绝借款总金额标准差

        apply_pass_order_amt_sum = rd_sub_df[rd_sub_df.status == 21].loan_amount.sum()  # 申请通过借款总金额和
        apply_pass_order_amt_max = rd_sub_df[rd_sub_df.status == 21].loan_amount.max()  # 申请通过借款总金额最大值
        apply_pass_order_amt_min = rd_sub_df[rd_sub_df.status == 21].loan_amount.min()  # 申请通过借款总金额最小值
        apply_pass_order_amt_avg = rd_sub_df[rd_sub_df.status == 21].loan_amount.mean()  # 申请通过借款总金额平均值
        apply_pass_order_amt_std = rd_sub_df[rd_sub_df.status == 21].loan_amount.std()  # 申请通过借款总金额标准差

        apply_rej_order_cnt_rate = fea_div_v2(apply_rej_order_cnt, apply_order_cnt)  # 申请放款拒绝率
        apply_pass_order_cnt_rate = fea_div_v2(apply_pass_order_cnt, apply_order_cnt)  # 申请放款成功率
        apply_rej_order_amt_rate = fea_div_v2(apply_rej_order_amt_sum, apply_order_amt)  # 申请放款拒绝金额占比
        apply_pass_order_amt_rate = fea_div_v2(apply_pass_order_amt_sum, apply_order_amt)  # 申请放款成功金额占比

        apply_app_cnt = rd_sub_df.acq_channel.nunique()  # 申请的app个数
        apply_app_pass_cnt = rd_sub_df[rd_sub_df.status == 21].acq_channel.nunique()  # 申请成功的app个数
        apply_app_rej_cnt = rd_sub_df[rd_sub_df.status == 22].acq_channel.nunique()  # 申请拒绝的app个数

        apply_product_cnt = rd_sub_df.product_code.nunique()  # 申请的product个数
        apply_product_pass_cnt = rd_sub_df[rd_sub_df.status == 21].product_code.nunique()  # 申请成功的product个数
        apply_product_rej_cnt = rd_sub_df[rd_sub_df.status == 22].product_code.nunique()  # 申请拒绝的product个数

        # 当前订单是否是首单 1 是首单 2 是再次单 0 是在当前产品是时间首单, 默认是首单 , 根据不同数据进行修改
        tmp1_rd_sub_df = rd_sub_df
        tmp1_rd_sub_df['last_status'] = tmp1_rd_sub_df['status'].shift(-1)
        tmp1_rd_sub_df['apply_if_again'] = tmp1_rd_sub_df['last_status'].apply(if_first_order)

        apply_order_type_first_cnt = len(tmp1_rd_sub_df[tmp1_rd_sub_df['apply_if_again'] == 1])  # 申请订单的首次订单总数次数
        apply_order_type_again_cnt = len(tmp1_rd_sub_df[tmp1_rd_sub_df['apply_if_again'] == 2])  # 申请订单的再次订单总数次数
        apply_order_type_first_rate = fea_div_v2(apply_order_type_first_cnt, apply_order_cnt)  # 申请订单的首次订单总数占比
        apply_order_type_again_rate = fea_div_v2(apply_order_type_again_cnt, apply_order_cnt)  # 申请订单的再次订单总数占比

        # 当前订单是否是首单 1 是首单 2 是再次单 0 是在当前产品是时间首单, 默认是首单 , 根据不同数据进行修改
        tmp2_rd_sub_df = rd_sub_df[rd_sub_df.status == 21]
        tmp2_rd_sub_df['last_status'] = tmp2_rd_sub_df['status'].shift(-1)
        tmp2_rd_sub_df['pass_if_again'] = tmp2_rd_sub_df['last_status'].apply(if_first_order)

        pass_order_type_first_cnt = len(tmp2_rd_sub_df[tmp2_rd_sub_df['pass_if_again'] == 1])  # 放款订单的首次订单总数次数
        pass_order_type_again_cnt = len(tmp2_rd_sub_df[tmp2_rd_sub_df['pass_if_again'] == 2])  # 放款订单的再次订单总数次数
        pass_order_type_first_rate = fea_div_v2(pass_order_type_first_cnt, apply_order_cnt)  # 放款订单的首次订单总数占比
        pass_order_type_again_rate = fea_div_v2(pass_order_type_again_cnt, apply_order_cnt)  # 放款订单的再次订单总数占比

        # 区分工作日和休息日week_day.isin([6,7])休息日 。time_type 1 上午 2 下午 3 晚上 4 凌晨， is_holiday， 1,0，
        week_day_df = rd_sub_df[rd_sub_df.week_day.isin([6, 7])]
        apply_week_day_cnt = len(week_day_df)  # 周末申请订单数
        apply_week_rej_order_cnt = len(week_day_df[week_day_df.status == 22])  # 周末申请拒绝借款总次数
        apply_week_pass_order_cnt = len(week_day_df[week_day_df.status == 21])  # 周末申请通过借款总次数
        apply_week_order_amt = week_day_df.loan_amount.sum()  # 周末申请借款总金额
        apply_week_rej_order_amt_sum = week_day_df[week_day_df.status == 22].loan_amount.sum()  # 周末申请拒绝借款总金额和
        apply_week_pass_order_amt_sum = week_day_df[week_day_df.status == 21].loan_amount.sum()  # 周末申请通过借款总金额和
        apply_week_day_cnt_rate = fea_div_v2(apply_week_day_cnt, apply_order_cnt)  # 周末申请的订单数占总订单数的比例
        apply_week_rej_order_cnt_rate = fea_div_v2(apply_week_rej_order_cnt,apply_rej_order_cnt)  # 周末申请的拒绝订单数占总拒绝订单数的比例
        apply_week_pass_order_cnt_rate = fea_div_v2(apply_week_pass_order_cnt,apply_pass_order_cnt)  # 周末申请的通过订单数占总拒绝订单数的比例
        apply_week_order_amt_rate = fea_div_v2(apply_week_order_amt, apply_order_amt)  # 周末申请的订单金额占总订单金额的比例
        apply_week_rej_order_amt_rate = fea_div_v2(apply_week_rej_order_amt_sum,apply_rej_order_amt_sum)  # 周末申请的拒绝订单金额占总订单拒绝金额的比例
        apply_week_pass_order_amt_rate = fea_div_v2(apply_week_pass_order_amt_sum,apply_pass_order_amt_sum)  # 周末申请的通过订单金额占总订单通过金额的比例

        work_day_df = rd_sub_df[~rd_sub_df.week_day.isin([6, 7])]
        apply_work_day_cnt = len(work_day_df)  # 工作日申请订单数
        apply_work_rej_order_cnt = len(work_day_df[work_day_df.status == 22])  # 工作日申请拒绝借款总次数
        apply_work_pass_order_cnt = len(work_day_df[work_day_df.status == 21])  # 工作日申请通过借款总次数
        apply_work_order_amt = work_day_df.loan_amount.sum()  # 工作日申请借款总金额
        apply_work_rej_order_amt_sum = work_day_df[work_day_df.status == 22].loan_amount.sum()  # 工作日申请拒绝借款总金额和
        apply_work_pass_order_amt_sum = work_day_df[work_day_df.status == 21].loan_amount.sum()  # 工作日申请通过借款总金额和
        apply_work_day_cnt_rate = fea_div_v2(apply_work_day_cnt, apply_order_cnt)  # 工作日申请的订单数占总订单数的比例
        apply_work_rej_order_cnt_rate = fea_div_v2(apply_work_rej_order_cnt,apply_rej_order_cnt)  # 工作日申请的拒绝订单数占总拒绝订单数的比例
        apply_work_pass_order_cnt_rate = fea_div_v2(apply_work_pass_order_cnt,apply_pass_order_cnt)  # 工作日申请的通过订单数占总拒绝订单数的比例
        apply_work_order_amt_rate = fea_div_v2(apply_work_order_amt, apply_order_amt)  # 工作日申请的订单金额占总订单金额的比例
        apply_work_rej_order_amt_rate = fea_div_v2(apply_work_rej_order_amt_sum,apply_rej_order_amt_sum)  # 工作日申请的拒绝订单金额占总订单拒绝金额的比例
        apply_work_pass_order_amt_rate = fea_div_v2(apply_work_pass_order_amt_sum,apply_pass_order_amt_sum)  # 工作日申请的通过订单金额占总订单通过金额的比例

        order_rd_feature_dict[f'{feature_type}_apply_order_cnt_{rd}r'] = apply_order_cnt
        order_rd_feature_dict[f'{feature_type}_apply_rej_order_cnt_{rd}r'] = apply_rej_order_cnt
        order_rd_feature_dict[f'{feature_type}_apply_pass_order_cnt_{rd}r'] = apply_pass_order_cnt
        order_rd_feature_dict[f'{feature_type}_apply_order_amt_{rd}r'] = apply_order_amt
        order_rd_feature_dict[f'{feature_type}_apply_rej_order_amt_sum_{rd}r'] = apply_rej_order_amt_sum
        order_rd_feature_dict[f'{feature_type}_apply_rej_order_amt_max_{rd}r'] = apply_rej_order_amt_max
        order_rd_feature_dict[f'{feature_type}_apply_rej_order_amt_min_{rd}r'] = apply_rej_order_amt_min
        order_rd_feature_dict[f'{feature_type}_apply_rej_order_amt_avg_{rd}r'] = apply_rej_order_amt_avg
        order_rd_feature_dict[f'{feature_type}_apply_rej_order_amt_std_{rd}r'] = apply_rej_order_amt_std
        order_rd_feature_dict[f'{feature_type}_apply_pass_order_amt_sum_{rd}r'] = apply_pass_order_amt_sum
        order_rd_feature_dict[f'{feature_type}_apply_pass_order_amt_max_{rd}r'] = apply_pass_order_amt_max
        order_rd_feature_dict[f'{feature_type}_apply_pass_order_amt_min_{rd}r'] = apply_pass_order_amt_min
        order_rd_feature_dict[f'{feature_type}_apply_pass_order_amt_avg_{rd}r'] = apply_pass_order_amt_avg
        order_rd_feature_dict[f'{feature_type}_apply_pass_order_amt_std_{rd}r'] = apply_pass_order_amt_std
        order_rd_feature_dict[f'{feature_type}_apply_rej_order_cnt_rate_{rd}r'] = apply_rej_order_cnt_rate
        order_rd_feature_dict[f'{feature_type}_apply_pass_order_cnt_rate_{rd}r'] = apply_pass_order_cnt_rate
        order_rd_feature_dict[f'{feature_type}_apply_rej_order_amt_rate_{rd}r'] = apply_rej_order_amt_rate
        order_rd_feature_dict[f'{feature_type}_apply_pass_order_amt_rate_{rd}r'] = apply_pass_order_amt_rate
        order_rd_feature_dict[f'{feature_type}_apply_app_cnt_{rd}r'] = apply_app_cnt
        order_rd_feature_dict[f'{feature_type}_apply_app_pass_cnt_{rd}r'] = apply_app_pass_cnt
        order_rd_feature_dict[f'{feature_type}_apply_app_rej_cnt_{rd}r'] = apply_app_rej_cnt
        order_rd_feature_dict[f'{feature_type}_apply_product_cnt_{rd}r'] = apply_product_cnt
        order_rd_feature_dict[f'{feature_type}_apply_product_pass_cnt_{rd}r'] = apply_product_pass_cnt
        order_rd_feature_dict[f'{feature_type}_apply_product_rej_cnt_{rd}r'] = apply_product_rej_cnt
        order_rd_feature_dict[f'{feature_type}_apply_order_type_first_cnt_{rd}r'] = apply_order_type_first_cnt
        order_rd_feature_dict[f'{feature_type}_apply_order_type_again_cnt_{rd}r'] = apply_order_type_again_cnt
        order_rd_feature_dict[f'{feature_type}_apply_order_type_first_rate_{rd}r'] = apply_order_type_first_rate
        order_rd_feature_dict[f'{feature_type}_apply_order_type_again_rate_{rd}r'] = apply_order_type_again_rate
        order_rd_feature_dict[f'{feature_type}_pass_order_type_first_cnt_{rd}r'] = pass_order_type_first_cnt
        order_rd_feature_dict[f'{feature_type}_pass_order_type_again_cnt_{rd}r'] = pass_order_type_again_cnt
        order_rd_feature_dict[f'{feature_type}_pass_order_type_first_rate_{rd}r'] = pass_order_type_first_rate
        order_rd_feature_dict[f'{feature_type}_pass_order_type_again_rate_{rd}r'] = pass_order_type_again_rate
        order_rd_feature_dict[f'{feature_type}_apply_week_day_cnt_{rd}r'] = apply_week_day_cnt
        order_rd_feature_dict[f'{feature_type}_apply_week_rej_order_cnt_{rd}r'] = apply_week_rej_order_cnt
        order_rd_feature_dict[f'{feature_type}_apply_week_pass_order_cnt_{rd}r'] = apply_week_pass_order_cnt
        order_rd_feature_dict[f'{feature_type}_apply_week_order_amt_{rd}r'] = apply_week_order_amt
        order_rd_feature_dict[f'{feature_type}_apply_week_rej_order_amt_sum_{rd}r'] = apply_week_rej_order_amt_sum
        order_rd_feature_dict[f'{feature_type}_apply_week_pass_order_amt_sum_{rd}r'] = apply_week_pass_order_amt_sum
        order_rd_feature_dict[f'{feature_type}_apply_week_day_cnt_rate_{rd}r'] = apply_week_day_cnt_rate
        order_rd_feature_dict[f'{feature_type}_apply_week_rej_order_cnt_rate_{rd}r'] = apply_week_rej_order_cnt_rate
        order_rd_feature_dict[f'{feature_type}_apply_week_pass_order_cnt_rate_{rd}r'] = apply_week_pass_order_cnt_rate
        order_rd_feature_dict[f'{feature_type}_apply_week_order_amt_rate_{rd}r'] = apply_week_order_amt_rate
        order_rd_feature_dict[f'{feature_type}_apply_week_rej_order_amt_rate_{rd}r'] = apply_week_rej_order_amt_rate
        order_rd_feature_dict[f'{feature_type}_apply_week_pass_order_amt_rate_{rd}r'] = apply_week_pass_order_amt_rate
        order_rd_feature_dict[f'{feature_type}_apply_work_day_cnt_{rd}r'] = apply_work_day_cnt
        order_rd_feature_dict[f'{feature_type}_apply_work_rej_order_cnt_{rd}r'] = apply_work_rej_order_cnt
        order_rd_feature_dict[f'{feature_type}_apply_work_pass_order_cnt_{rd}r'] = apply_work_pass_order_cnt
        order_rd_feature_dict[f'{feature_type}_apply_work_order_amt_{rd}r'] = apply_work_order_amt
        order_rd_feature_dict[f'{feature_type}_apply_work_rej_order_amt_sum_{rd}r'] = apply_work_rej_order_amt_sum
        order_rd_feature_dict[f'{feature_type}_apply_work_pass_order_amt_sum_{rd}r'] = apply_work_pass_order_amt_sum
        order_rd_feature_dict[f'{feature_type}_apply_work_day_cnt_rate_{rd}r'] = apply_work_day_cnt_rate
        order_rd_feature_dict[f'{feature_type}_apply_work_rej_order_cnt_rate_{rd}r'] = apply_work_rej_order_cnt_rate
        order_rd_feature_dict[f'{feature_type}_apply_work_pass_order_cnt_rate_{rd}r'] = apply_work_pass_order_cnt_rate
        order_rd_feature_dict[f'{feature_type}_apply_work_order_amt_rate_{rd}r'] = apply_work_order_amt_rate
        order_rd_feature_dict[f'{feature_type}_apply_work_rej_order_amt_rate_{rd}r'] = apply_work_rej_order_amt_rate
        order_rd_feature_dict[f'{feature_type}_apply_work_pass_order_amt_rate_{rd}r'] = apply_work_pass_order_amt_rate

        for tp in [1, 2, 3, 4]:
            tp_day_df = rd_sub_df[rd_sub_df.time_type == tp]
            apply_tp_day_cnt = len(tp_day_df)  # 某个时间段申请订单数
            apply_tp_rej_order_cnt = len(tp_day_df[tp_day_df.status == 22])  # 某个时间段申请拒绝借款总次数
            apply_tp_pass_order_cnt = len(tp_day_df[tp_day_df.status == 21])  # 某个时间段申请通过借款总次数
            apply_tp_order_amt = tp_day_df.loan_amount.sum()  # 某个时间段申请借款总金额
            apply_tp_rej_order_amt_sum = tp_day_df[tp_day_df.status == 22].loan_amount.sum()  # 某个时间段申请拒绝借款总金额和
            apply_tp_pass_order_amt_sum = tp_day_df[tp_day_df.status == 21].loan_amount.sum()  # 某个时间段申请通过借款总金额和
            apply_tp_day_cnt_rate = fea_div_v2(apply_tp_day_cnt, apply_order_cnt)  # 某个时间段申请的订单数占总订单数的比例
            apply_tp_rej_order_cnt_rate = fea_div_v2(apply_tp_rej_order_cnt,apply_rej_order_cnt)  # 某个时间段申请的拒绝订单数占总拒绝订单数的比例
            apply_tp_pass_order_cnt_rate = fea_div_v2(apply_tp_pass_order_cnt,apply_pass_order_cnt)  # 某个时间段申请的通过订单数占总拒绝订单数的比例
            apply_tp_order_amt_rate = fea_div_v2(apply_tp_order_amt, apply_order_amt)  # 某个时间段申请的订单金额占总订单金额的比例
            apply_tp_rej_order_amt_rate = fea_div_v2(apply_tp_rej_order_amt_sum,apply_rej_order_amt_sum)  # 某个时间段申请的拒绝订单金额占总订单拒绝金额的比例
            apply_tp_pass_order_amt_rate = fea_div_v2(apply_tp_pass_order_amt_sum,apply_pass_order_amt_sum)  # 某个时间段申请的通过订单金额占总订单通过金额的比例

            order_rd_feature_dict[f'{feature_type}_apply_tp{tp}_day_cnt_{rd}r'] = apply_tp_day_cnt
            order_rd_feature_dict[f'{feature_type}_apply_tp{tp}_rej_order_cnt_{rd}r'] = apply_tp_rej_order_cnt
            order_rd_feature_dict[f'{feature_type}_apply_tp{tp}_pass_order_cnt_{rd}r'] = apply_tp_pass_order_cnt
            order_rd_feature_dict[f'{feature_type}_apply_tp{tp}_order_amt_{rd}r'] = apply_tp_order_amt
            order_rd_feature_dict[f'{feature_type}_apply_tp{tp}_rej_order_amt_sum_{rd}r'] = apply_tp_rej_order_amt_sum
            order_rd_feature_dict[f'{feature_type}_apply_tp{tp}_pass_order_amt_sum_{rd}r'] = apply_tp_pass_order_amt_sum
            order_rd_feature_dict[f'{feature_type}_apply_tp{tp}_day_cnt_rate_{rd}r'] = apply_tp_day_cnt_rate
            order_rd_feature_dict[f'{feature_type}_apply_tp{tp}_rej_order_cnt_rate_{rd}r'] = apply_tp_rej_order_cnt_rate
            order_rd_feature_dict[f'{feature_type}_apply_tp{tp}_pass_order_cnt_rate_{rd}r'] = apply_tp_pass_order_cnt_rate
            order_rd_feature_dict[f'{feature_type}_apply_tp{tp}_order_amt_rate_{rd}r'] = apply_tp_order_amt_rate
            order_rd_feature_dict[f'{feature_type}_apply_tp{tp}_rej_order_amt_rate_{rd}r'] = apply_tp_rej_order_amt_rate
            order_rd_feature_dict[f'{feature_type}_apply_tp{tp}_pass_order_amt_rate_{rd}r'] = apply_tp_pass_order_amt_rate


        for hy in [0, 1]:
            hy_day_df = rd_sub_df[rd_sub_df.is_holiday == hy]
            apply_hy_day_cnt = len(hy_day_df)  # 节假日时间段申请订单数
            apply_hy_rej_order_cnt = len(hy_day_df[hy_day_df.status == 22])  # 节假日时间段申请拒绝借款总次数
            apply_hy_pass_order_cnt = len(hy_day_df[hy_day_df.status == 21])  # 节假日时间段申请通过借款总次数
            apply_hy_order_amt = hy_day_df.loan_amount.sum()  # 节假日时间段申请借款总金额
            apply_hy_rej_order_amt_sum = hy_day_df[hy_day_df.status == 22].loan_amount.sum()  # 节假日时间段申请拒绝借款总金额和
            apply_hy_pass_order_amt_sum = hy_day_df[hy_day_df.status == 21].loan_amount.sum()  # 节假日时间段申请通过借款总金额和
            apply_hy_day_cnt_rate = fea_div_v2(apply_hy_day_cnt, apply_order_cnt)  # 节假日时间段申请的订单数占总订单数的比例
            apply_hy_rej_order_cnt_rate = fea_div_v2(apply_hy_rej_order_cnt,apply_rej_order_cnt)  # 节假日时间段申请的拒绝订单数占总拒绝订单数的比例
            apply_hy_pass_order_cnt_rate = fea_div_v2(apply_hy_pass_order_cnt,apply_pass_order_cnt)  # 节假日时间段申请的通过订单数占总拒绝订单数的比例
            apply_hy_order_amt_rate = fea_div_v2(apply_hy_order_amt, apply_order_amt)  # 节假日时间段申请的订单金额占总订单金额的比例
            apply_hy_rej_order_amt_rate = fea_div_v2(apply_hy_rej_order_amt_sum,apply_rej_order_amt_sum)  # 节假日时间段申请的拒绝订单金额占总订单拒绝金额的比例
            apply_hy_pass_order_amt_rate = fea_div_v2(apply_hy_pass_order_amt_sum,apply_pass_order_amt_sum)  # 节假日时间段申请的通过订单金额占总订单通过金额的比例

            order_rd_feature_dict[f'{feature_type}_apply_hy{hy}_day_cnt_{rd}r'] = apply_hy_day_cnt
            order_rd_feature_dict[f'{feature_type}_apply_hy{hy}_rej_order_cnt_{rd}r'] = apply_hy_rej_order_cnt
            order_rd_feature_dict[f'{feature_type}_apply_hy{hy}_pass_order_cnt_{rd}r'] = apply_hy_pass_order_cnt
            order_rd_feature_dict[f'{feature_type}_apply_hy{hy}_order_amt_{rd}r'] = apply_hy_order_amt
            order_rd_feature_dict[f'{feature_type}_apply_hy{hy}_rej_order_amt_sum_{rd}r'] = apply_hy_rej_order_amt_sum
            order_rd_feature_dict[f'{feature_type}_apply_hy{hy}_pass_order_amt_sum_{rd}r'] = apply_hy_pass_order_amt_sum
            order_rd_feature_dict[f'{feature_type}_apply_hy{hy}_day_cnt_rate_{rd}r'] = apply_hy_day_cnt_rate
            order_rd_feature_dict[f'{feature_type}_apply_hy{hy}_rej_order_cnt_rate_{rd}r'] = apply_hy_rej_order_cnt_rate
            order_rd_feature_dict[f'{feature_type}_apply_hy{hy}_pass_order_cnt_rate_{rd}r'] = apply_hy_pass_order_cnt_rate
            order_rd_feature_dict[f'{feature_type}_apply_hy{hy}_order_amt_rate_{rd}r'] = apply_hy_order_amt_rate
            order_rd_feature_dict[f'{feature_type}_apply_hy{hy}_rej_order_amt_rate_{rd}r'] = apply_hy_rej_order_amt_rate
            order_rd_feature_dict[f'{feature_type}_apply_hy{hy}_pass_order_amt_rate_{rd}r'] = apply_hy_pass_order_amt_rate

    return order_rd_feature_dict


def get_order_continue_features(user_history_order,
                                day_list=[1, 3, 7, 15, 30, 60, 90, 180, 'all'],
                                current_apply_time=None,
                                feature_type='all'):
    order_cts_feature_dict = {}

    if feature_type == 'all':
        user_history_order = user_history_order.copy()
    elif 'app' in feature_type:
        user_history_order = user_history_order[user_history_order.app_type == feature_type]
    elif 'product' in feature_type:
        user_history_order = user_history_order[user_history_order.product_type == feature_type]
    for day in day_list:
        if day == 'all':
            day_sub_df = user_history_order.copy()  # 最近N天的申请数据
        else:
            day_sub_df = user_history_order[user_history_order.order_day_inter <= day].copy()  # 最近N天的申请数据

        status_list = day_sub_df.status.tolist()
        continues_dict = find_max_consecutive(status_list)
        if 21 in list(continues_dict.keys()):
            pass_order_st = continues_dict[21][0]
            pass_order_ed = continues_dict[21][1]
            pass_continues_order = day_sub_df.iloc[pass_order_st:pass_order_ed, :]
            continue_min_amt = pass_continues_order.loan_amount.min()
            continue_max_amt = pass_continues_order.loan_amount.min()
            pass_order_st_time = day_sub_df.iloc[pass_order_st, :].apply_time
            pass_order_ed_time = day_sub_df.iloc[pass_order_ed, :].apply_time

            pass_current_st_day_inter = get_days_inter(current_apply_time,pass_order_st_time)  # 订单连续放款 start time 距离 本次订单申请时间差
            pass_current_ed_day_inter = get_days_inter(current_apply_time,pass_order_ed_time)  # 订单连续放款 end time 距离 本次订单申请时间差
            pass_order_continue_times = pass_order_ed - pass_order_st  # 订单连续放款次数
            pass_order_continue_days = get_days_inter(pass_order_ed_time,pass_order_st_time)  # 连续放款 订单 的start time 和end time 的时间差
            pass_order_continue_amt_diff = continue_max_amt - continue_min_amt  # 连续放款订单最小金额和最大金额差
            pass_order_continue_cnt_rate = fea_div_v2(pass_order_continue_times,pass_order_continue_days)  # 连续放款订单 的订单数/ 连续放款 订单 的start time 和end time 的时间差
        else:
            pass_current_st_day_inter = -999
            pass_current_ed_day_inter = -999
            pass_order_continue_times = -999
            pass_order_continue_days = -999
            pass_order_continue_amt_diff = -999
            pass_order_continue_cnt_rate = -999

        if 22 in list(continues_dict.keys()):
            rej_order_st = continues_dict[22][0]
            rej_order_ed = continues_dict[22][1]
            rej_continues_order = day_sub_df.iloc[rej_order_st:rej_order_ed, :]
            continue_min_amt = rej_continues_order.loan_amount.min()
            continue_max_amt = rej_continues_order.loan_amount.min()
            rej_order_st_time = day_sub_df.iloc[rej_order_st, :].apply_time
            rej_order_ed_time = day_sub_df.iloc[rej_order_ed, :].apply_time

            rej_current_st_day_inter = get_days_inter(current_apply_time,rej_order_st_time)  # 订单连续拒绝 start time 距离 本次订单申请时间差
            rej_current_ed_day_inter = get_days_inter(current_apply_time,rej_order_ed_time)  # 订单连续拒绝 end time 距离 本次订单申请时间差
            rej_order_continue_times = rej_order_ed - rej_order_st  # 订单连续拒绝次数
            rej_order_continue_days = get_days_inter(rej_order_ed_time,rej_order_st_time)  # 连续拒绝 订单 的start time 和end time 的时间差
            rej_order_continue_amt_diff = continue_max_amt - continue_min_amt  # 连续拒绝订单最小金额和最大金额差
            rej_order_continue_cnt_rate = fea_div_v2(rej_order_continue_times,rej_order_continue_days)  # 连续拒绝订单 的订单数/ 连续拒绝 订单 的start time 和end time 的时间差
        else:
            rej_current_st_day_inter = -999
            rej_current_ed_day_inter = -999
            rej_order_continue_times = -999
            rej_order_continue_days = -999
            rej_order_continue_amt_diff = -999
            rej_order_continue_cnt_rate = -999

        order_cts_feature_dict[f'{feature_type}_apply_rej_current_st_day_inter_{day}d'] = rej_current_st_day_inter
        order_cts_feature_dict[f'{feature_type}_apply_rej_current_ed_day_inter_{day}d'] = rej_current_ed_day_inter
        order_cts_feature_dict[f'{feature_type}_apply_rej_order_continue_times_{day}d'] = rej_order_continue_times
        order_cts_feature_dict[f'{feature_type}_apply_rej_order_continue_days_{day}d'] = rej_order_continue_days
        order_cts_feature_dict[f'{feature_type}_apply_rej_order_continue_amt_diff_{day}d'] = rej_order_continue_amt_diff
        order_cts_feature_dict[f'{feature_type}_apply_rej_order_continue_cnt_rate_{day}d'] = rej_order_continue_cnt_rate
        order_cts_feature_dict[f'{feature_type}_apply_pass_current_st_day_inter_{day}d'] = pass_current_st_day_inter
        order_cts_feature_dict[f'{feature_type}_apply_pass_current_ed_day_inter_{day}d'] = pass_current_ed_day_inter
        order_cts_feature_dict[f'{feature_type}_apply_pass_order_continue_times_{day}d'] = pass_order_continue_times
        order_cts_feature_dict[f'{feature_type}_apply_pass_order_continue_days_{day}d'] = pass_order_continue_days
        order_cts_feature_dict[f'{feature_type}_apply_pass_order_continue_amt_diff_{day}d'] = pass_order_continue_amt_diff
        order_cts_feature_dict[f'{feature_type}_apply_pass_order_continue_cnt_rate_{day}d'] = pass_order_continue_cnt_rate

    return order_cts_feature_dict


def get_day_agg_rst(user_history_order):
    all_day_agg = user_history_order.groupby(['app_day']).agg(amount_sum=('loan_amount', 'sum'),
                                                              order_num=('app_order_id', 'count'),
                                                              product_code_num=('product_code', 'nunique'),
                                                              acq_channel_num=('acq_channel', 'nunique')).reset_index()

    pass_day_agg = user_history_order[user_history_order.status == 21].groupby(['app_day']).agg(
        pass_amount_sum=('loan_amount', 'sum'),
        pass_order_num=('app_order_id', 'count'),
        pass_product_code_num=('product_code', 'nunique'),
        pass_acq_channel_num=('acq_channel', 'nunique')).reset_index()

    rej_day_agg = user_history_order[user_history_order.status == 22].groupby(['app_day']).agg(
        rej_amount_sum=('loan_amount', 'sum'),
        rej_order_num=('app_order_id', 'count'),
        rej_product_code_num=('product_code', 'nunique'),
        rej_acq_channel_num=('acq_channel', 'nunique')).reset_index()
    all_day_agg = all_day_agg.merge(pass_day_agg, on='app_day', how='left')
    all_day_agg = all_day_agg.merge(rej_day_agg, on='app_day', how='left')
    all_day_agg.fillna(0, inplace=True)
    return all_day_agg


def get_order_day_features(user_history_order,
                           day_list=[1, 3, 7, 15, 30, 60, 90, 180, 'all'],
                           current_apply_time=None,
                           feature_type='all'):
    order_day_feature_dict = {}

    if feature_type == 'all':
        user_history_order = user_history_order.copy()
    elif 'app' in feature_type:
        user_history_order = user_history_order[user_history_order.app_type == feature_type]
    elif 'product' in feature_type:
        user_history_order = user_history_order[user_history_order.product_type == feature_type]
    for day in day_list:
        if day == 'all':
            day_sub_df = user_history_order.copy()  # 最近N天的申请数据
        else:
            day_sub_df = user_history_order[user_history_order.order_day_inter <= day].copy()  # 最近N天的申请数据
        rst_df = get_day_agg_rst(day_sub_df)
        # 所有申请单
        if len(rst_df) > 0:
            all_order = rst_df.app_day.to_list()
            all_order.append(current_apply_time[:10])
            date_objects = [datetime.strptime(date, '%Y-%m-%d') for date in all_order]
            intervals = [(date_objects[i + 1] - date_objects[i]).days for i in range(len(date_objects) - 1)]
            max_iter = (date_objects[-1] - date_objects[0]).days
            apply_day_contact_density = fea_div_v2(math.sqrt(sum([math.pow(i, 2) for i in intervals])), max_iter)  # 第一次订单申请 到本次订单申请 时间内申请订单按天聚合的联系密度
            apply_day_inter_max = max(intervals)  # 申请订单按天聚合最大空档天数
            apply_day_inter_min = min(intervals)  # 申请订单按天聚合最小空档天数
            apply_day_inter_avg = np.mean(intervals)  # 申请订单按天聚合平均空挡天数
            apply_day_inter_std = np.std(intervals)  # 申请订单按天聚合空档天数标准差
            apply_day_inter_max_rate = fea_div_v2(apply_day_inter_max, max_iter)  # 申请订单按天聚合最大空档天数/(首次申请 - 当前申请）天数差
            apply_day_order_cnt_max = rst_df.order_num.max()  # 按天聚合 一天内的申请订单数的最大值
            apply_day_order_cnt_min = rst_df.order_num.min()  # 按天聚合 一天内的申请订单数的最小值
            apply_day_order_cnt_avg = rst_df.order_num.mean()  # 按天聚合 一天内的申请订单数的平均值
            apply_day_order_cnt_std = rst_df.order_num.std()  # 按天聚合 一天内的申请订单数的标准差
            apply_day_order_amt_max = rst_df.amount_sum.max()  # 按天聚合 一天内的申请订单金额的最大值
            apply_day_order_amt_min = rst_df.amount_sum.min()  # 按天聚合 一天内的申请订单金额的最小值
            apply_day_order_amt_avg = rst_df.amount_sum.mean()  # 按天聚合 一天内的申请订单金额的平均值
            apply_day_order_amt_std = rst_df.amount_sum.std()  # 按天聚合 一天内的申请订单金额的标准差
            # 所有放款订单
            pass_order = rst_df[rst_df.pass_amount_sum > 0].app_day.to_list()
            if len(pass_order) > 0:
                pass_order.append(current_apply_time[:10])
                date_objects = [datetime.strptime(date, '%Y-%m-%d') for date in pass_order]
                intervals = [(date_objects[i + 1] - date_objects[i]).days for i in range(len(date_objects) - 1)]
                max_iter = (date_objects[-1] - date_objects[0]).days
                apply_day_pass_contact_density = fea_div_v2(math.sqrt(sum([math.pow(i, 2) for i in intervals])), max_iter)  # 第一次订单申请 到本次订单申请 时间内放款订单按天聚合的联系密度
                apply_day_pass_inter_max = max(intervals)  # 放款订单按天聚合最大空档天数
                apply_day_pass_inter_min = min(intervals)  # 放款订单按天聚合最小空档天数
                apply_day_pass_inter_avg = np.mean(intervals)  # 放款订单按天聚合平均空挡天数
                apply_day_pass_inter_std = np.std(intervals)  # 放款订单按天聚合空档天数标准差
                apply_day_pass_inter_max_rate = fea_div_v2(apply_day_inter_max, max_iter)  # 放款订单按天聚合最大空档天数/(首次申请 - 当前申请）天数差
                apply_day_pass_order_cnt_max = rst_df[rst_df.pass_amount_sum > 0].order_num.max()  # 按天聚合 一天内的放款订单数的最大值
                apply_day_pass_order_cnt_min = rst_df[rst_df.pass_amount_sum > 0].order_num.min()  # 按天聚合 一天内的放款订单数的最小值
                apply_day_pass_order_cnt_avg = rst_df[rst_df.pass_amount_sum > 0].order_num.mean()  # 按天聚合 一天内的放款订单数的平均值
                apply_day_pass_order_cnt_std = rst_df[rst_df.pass_amount_sum > 0].order_num.std()  # 按天聚合 一天内的放款订单数的标准差
                apply_day_pass_order_amt_max = rst_df[rst_df.pass_amount_sum > 0].amount_sum.max()  # 按天聚合 一天内的放款订单金额的最大值
                apply_day_pass_order_amt_min = rst_df[rst_df.pass_amount_sum > 0].amount_sum.min()  # 按天聚合 一天内的放款订单金额的最小值
                apply_day_pass_order_amt_avg = rst_df[rst_df.pass_amount_sum > 0].amount_sum.mean()  # 按天聚合 一天内的放款订单金额的平均值
                apply_day_pass_order_amt_std = rst_df[rst_df.pass_amount_sum > 0].amount_sum.std()  # 按天聚合 一天内的放款订单金额的标准差
            else:
                apply_day_pass_contact_density = -999
                apply_day_pass_inter_max = -999
                apply_day_pass_inter_min = -999
                apply_day_pass_inter_avg = -999
                apply_day_pass_inter_std = -999
                apply_day_pass_inter_max_rate = -999
                apply_day_pass_order_cnt_max = -999
                apply_day_pass_order_cnt_min = -999
                apply_day_pass_order_cnt_avg = -999
                apply_day_pass_order_cnt_std = -999
                apply_day_pass_order_amt_max = -999
                apply_day_pass_order_amt_min = -999
                apply_day_pass_order_amt_avg = -999
                apply_day_pass_order_amt_std = -999
        else:
            apply_day_contact_density = -999
            apply_day_inter_max = -999
            apply_day_inter_min = -999
            apply_day_inter_avg = -999
            apply_day_inter_std = -999
            apply_day_inter_max_rate = -999
            apply_day_order_cnt_max = -999
            apply_day_order_cnt_min = -999
            apply_day_order_cnt_avg = -999
            apply_day_order_cnt_std = -999
            apply_day_order_amt_max = -999
            apply_day_order_amt_min = -999
            apply_day_order_amt_avg = -999
            apply_day_order_amt_std = -999
            apply_day_pass_contact_density = -999
            apply_day_pass_inter_max = -999
            apply_day_pass_inter_min = -999
            apply_day_pass_inter_avg = -999
            apply_day_pass_inter_std = -999
            apply_day_pass_inter_max_rate = -999
            apply_day_pass_order_cnt_max = -999
            apply_day_pass_order_cnt_min = -999
            apply_day_pass_order_cnt_avg = -999
            apply_day_pass_order_cnt_std = -999
            apply_day_pass_order_amt_max = -999
            apply_day_pass_order_amt_min = -999
            apply_day_pass_order_amt_avg = -999
            apply_day_pass_order_amt_std = -999

        order_day_feature_dict[f'{feature_type}_apply_day_contact_density_{day}d'] = apply_day_contact_density
        order_day_feature_dict[f'{feature_type}_apply_day_inter_max_{day}d'] = apply_day_inter_max
        order_day_feature_dict[f'{feature_type}_apply_day_inter_min_{day}d'] = apply_day_inter_min
        order_day_feature_dict[f'{feature_type}_apply_day_inter_avg_{day}d'] = apply_day_inter_avg
        order_day_feature_dict[f'{feature_type}_apply_day_inter_std_{day}d'] = apply_day_inter_std
        order_day_feature_dict[f'{feature_type}_apply_day_inter_max_rate_{day}d'] = apply_day_inter_max_rate
        order_day_feature_dict[f'{feature_type}_apply_day_order_cnt_max_{day}d'] = apply_day_order_cnt_max
        order_day_feature_dict[f'{feature_type}_apply_day_order_cnt_min_{day}d'] = apply_day_order_cnt_min
        order_day_feature_dict[f'{feature_type}_apply_day_order_cnt_avg_{day}d'] = apply_day_order_cnt_avg
        order_day_feature_dict[f'{feature_type}_apply_day_order_cnt_std_{day}d'] = apply_day_order_cnt_std
        order_day_feature_dict[f'{feature_type}_apply_day_order_amt_max_{day}d'] = apply_day_order_amt_max
        order_day_feature_dict[f'{feature_type}_apply_day_order_amt_min_{day}d'] = apply_day_order_amt_min
        order_day_feature_dict[f'{feature_type}_apply_day_order_amt_avg_{day}d'] = apply_day_order_amt_avg
        order_day_feature_dict[f'{feature_type}_apply_day_order_amt_std_{day}d'] = apply_day_order_amt_std
        order_day_feature_dict[f'{feature_type}_apply_day_pass_contact_density_{day}d'] = apply_day_pass_contact_density
        order_day_feature_dict[f'{feature_type}_apply_day_pass_inter_max_{day}d'] = apply_day_pass_inter_max
        order_day_feature_dict[f'{feature_type}_apply_day_pass_inter_min_{day}d'] = apply_day_pass_inter_min
        order_day_feature_dict[f'{feature_type}_apply_day_pass_inter_avg_{day}d'] = apply_day_pass_inter_avg
        order_day_feature_dict[f'{feature_type}_apply_day_pass_inter_std_{day}d'] = apply_day_pass_inter_std
        order_day_feature_dict[f'{feature_type}_apply_day_pass_inter_max_rate_{day}d'] = apply_day_pass_inter_max_rate
        order_day_feature_dict[f'{feature_type}_apply_day_pass_order_cnt_max_{day}d'] = apply_day_pass_order_cnt_max
        order_day_feature_dict[f'{feature_type}_apply_day_pass_order_cnt_min_{day}d'] = apply_day_pass_order_cnt_min
        order_day_feature_dict[f'{feature_type}_apply_day_pass_order_cnt_avg_{day}d'] = apply_day_pass_order_cnt_avg
        order_day_feature_dict[f'{feature_type}_apply_day_pass_order_cnt_std_{day}d'] = apply_day_pass_order_cnt_std
        order_day_feature_dict[f'{feature_type}_apply_day_pass_order_amt_max_{day}d'] = apply_day_pass_order_amt_max
        order_day_feature_dict[f'{feature_type}_apply_day_pass_order_amt_min_{day}d'] = apply_day_pass_order_amt_min
        order_day_feature_dict[f'{feature_type}_apply_day_pass_order_amt_avg_{day}d'] = apply_day_pass_order_amt_avg
        order_day_feature_dict[f'{feature_type}_apply_day_pass_order_amt_std_{day}d'] = apply_day_pass_order_amt_std

    return order_day_feature_dict
